9. Palindrome Number
easyDecide whether an integer reads the same forward and backward — without converting it to a string. A classic warm-up that tests digit-reversal arithmetic and overflow awareness.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given an integer x, return true if x is a palindrome, and false otherwise. An integer is a palindrome when it reads the same forward and backward. Follow up: Could you solve it without converting the integer to a string?
Constraints
-2^31 <= x <= 2^31 - 1
Examples
Example 1
x = 121trueExplanation: 121 reads as 121 from left to right and from right to left.
Example 2
x = -121falseExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3
x = 10falseExplanation: Reads 01 from right to left. Therefore it is not a palindrome.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Hints
Progressive — try the first before opening the next.
Hint 1
Negative numbers can never be palindromes. Numbers ending in 0 (except 0 itself) also can't be — the leading digit can never be 0.
Hint 2
The string-conversion approach is one line but the follow-up asks you to avoid it. How would you compare digits arithmetically?
Hint 3
You only need to reverse half the number. Keep popping the last digit of x into a reversed half until reversed >= x. Then compare.
Hint 4
When reversed == x (even-length) or reversed / 10 == x (odd-length, middle digit drops out), it's a palindrome.
Solution approach
Reveal approach
Early exit on negatives and on positive numbers ending in 0 (except 0 itself). Then build the reversed half: while x > reversed, peel x's last digit (digit = x % 10, reversed = reversed * 10 + digit, x /= 10). Stop when reversed >= x — you've crossed the midpoint. For even-length numbers x == reversed; for odd-length the middle digit sits alone in reversed so check x == reversed / 10. This avoids overflowing on full reversal of large 10-digit ints. O(log10(x)) time, O(1) space.
Complexity
- Time
- O(log n)
- Space
- O(1)
Related patterns
- math
- two-pointers
Related problems
- 7. Reverse Integer
- 125. Valid Palindrome
Asked at
Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).
- Amazon
- Microsoft
- Apple
- Bloomberg
Practice these live with InterviewChamp.AI
Drill Palindrome Number and Math problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →