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
Constraints
- ●
-2^31 <= x <= 2^31 - 1
Examples
x = 121truex = -121falsex = 10falseAsked At
4 companies| Company | Difficulty | |
|---|---|---|
| Easy | View all Google questions → | |
| Amazon | Easy | View all Amazon questions → |
| Microsoft | Easy | View all Microsoft questions → |
| Bloomberg | Easy | View all Bloomberg questions → |
How to Think About It
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.
Key insight: reverse the second half of the number and compare it to the first half. If they match, the number is a palindrome.
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.
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).
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
What Trips People Up in Real Interviews
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.
Solution Code
O(log n) timedef 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 // 10Frequently 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.