EasyMathUpdated Sep 2026

9. Palindrome Number

Asked at Google, Amazon, Microsoft, Bloomberg

Difficulty

Easy

Time

O(log n)

Space

O(1)

Pattern

Math

Companies

4

Problem

You are given an integer x. Decide whether x is a palindrome: a number that reads the same from left to right and from right to left. Return true if it is, false if it is not.

A negative number is never a palindrome, because the minus sign only appears on the left side. Also, any positive number that ends in 0 cannot be a palindrome, since a palindrome cannot start with 0 (except the number 0 itself).

The follow-up asks you to solve it without turning the number into a string. The trick is to reverse only the second half of the digits and compare it with the first half, so you never build a full copy of the number.

Same digits, both directions

x = 122112210123The outer pair 1 and 1 match.x = 122112210123The inner pair 2 and 2 also match, so 1221 is apalindrome.
For 1221 the digits read the same from the left and from the right, so the answer is true.

Constraints

  • ●-2^31 <= x <= 2^31 - 1

Examples

Example 1
Inputx = 121
Outputtrue
WhyReading 121 forwards gives 1, 2, 1 and reading it backwards gives the same 1, 2, 1, so it is a palindrome.
Example 2
Inputx = -121
Outputfalse
WhyThe minus sign sits only on the left. Reversed it would appear on the right, so the two readings differ.
Example 3
Inputx = 10
Outputfalse
WhyReversed, 10 becomes 01, which is just 1. Since 10 and 1 are different, it is not a palindrome.

Asked At

4 companies

How to Think About It

1

Brute force: convert to string, check if it reads the same forwards and backwards. O(n) time and space. The interviewer will ask you to avoid string conversion.

2

Key insight: reverse the second half of the number and compare it to the first half. If they match, the number is a palindrome.

3

Why reverse half: for 1221, reversing the last two digits gives 12. Compare 12 == 12 (first half). For 12321, reversing gives 123. Compare 12 == 123 // 10 = 12.

4

How to know when half is reversed: when reversed >= original, you have processed at least half the digits. For even-length numbers, reversed == original. For odd-length, reversed // 10 == original (ignore the middle digit).

5

Edge cases: negative numbers (not palindromes), numbers ending in 0 (not palindromes unless 0 itself), single digit (always palindrome).

Optimal Approach

Step 1: If x < 0 or (x % 10 == 0 and x != 0), return false.
Step 2: Initialize reversed = 0.
Step 3: While x > reversed:
reversed = reversed * 10 + x % 10
x //= 10
Step 4: Return x == reversed or x == reversed // 10.

The second condition in step 4 handles odd-length numbers where the middle digit is in reversed but not in x.

Time: O(log n) — we process half the digits. Space: O(1) — no extra data structures.

Reverse only the second half

x = 122112210123xreversedStart: x = 1221, reversed = 0.x = 122112210123xreversedAfter one step: x = 122, reversed = 1.x = 122112210123xreversedAfter two steps: x = 12, reversed = 12. They areequal, so stop.reversed = reversed * 10 + x % 10x = x // 10stop when reversed >= xeven length: x == reversedodd length: x == reversed // 10
We peel digits off the right of x and push them onto reversed until reversed catches up with x.
ComplexityTime O(log n)Space O(1)We touch only about half of the digits of x, and we keep just a couple of integer variables.

What Trips People Up in Real Interviews

1

Converting to a string. The problem asks for a solution without string conversion. Use the half-reversal approach.

2

Forgetting to handle negative numbers. All negative numbers are not palindromes because of the minus sign.

3

Not handling numbers ending in 0. A number like 10 is not a palindrome. Check x % 10 == 0 and x != 0 at the start.

4

Comparing the full reversed number to the original. For odd-length numbers, the middle digit ends up in reversed but not in x. Compare x == reversed // 10 to ignore it.

5

Overflow when reversing. Since we only reverse half the digits, the reversed value stays within int range. No overflow risk.

Solution Code

O(log n) time
def isPalindrome(x):
    if x < 0 or (x % 10 == 0 and x != 0):
        return False
    reversed = 0
    while x > reversed:
        reversed = reversed * 10 + x % 10
        x //= 10
    return x == reversed or x == reversed // 10

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Palindrome Number problem?

Determine whether an integer is a palindrome without converting it to a string. The optimal approach reverses only half of the number and compares, avoiding `O(n)` space for string conversion.

How do you solve Palindrome Number?

The optimal approach runs in O(log n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Palindrome Number?

Palindrome Number is asked at Google, Amazon, Microsoft, Bloomberg. It is a easy difficulty problem.

What are common mistakes on Palindrome Number?
  • Converting to a string. The problem asks for a solution without string conversion. Use the half-reversal approach.
  • Forgetting to handle negative numbers. All negative numbers are not palindromes because of the minus sign.
  • Not handling numbers ending in 0. A number like 10 is not a palindrome. Check `x % 10 == 0 and x != 0` at the start.
  • Comparing the full reversed number to the original. For odd-length numbers, the middle digit ends up in `reversed` but not in `x`. Compare `x == reversed // 10` to ignore it.
  • Overflow when reversing. Since we only reverse half the digits, the reversed value stays within `int` range. No overflow risk.