- Interview Questions
- /
- Problems
- /
- Palindrome Number
Palindrome Number
Decide whether an integer reads the same forwards and backwards. Easy with strings; the interesting version reverses half the digits with arithmetic. Solve it below in Python or JavaScript, then read the walkthrough.
Problem
Given an integer x, return true if its decimal digits read the same from left to right as from right to left, and false otherwise.
The minus sign counts as part of how the number is written, so a negative number is never a palindrome: -121 backwards would be 121-.
Try to solve it without converting the number to a string.
Example 1
- Input
- x = 4554
- Output
- true
Reversing the digits 4, 5, 5, 4 gives the same sequence.
Example 2
- Input
- x = -373
- Output
- false
Written backwards it would be "373-", which is not the same.
Example 3
- Input
- x = 560
- Output
- false
Reversed it would start with a 0 ("065"), and no positive integer is written with a leading zero.
Constraints
- -2³¹ ≤ x ≤ 2³¹ - 1
- x is an integer; no leading zeros appear in its written form (except the number 0 itself).
Practice
Runs 13 tests in your browser. Python loads on the first run (about 6 MB, then cached). Press ⌘/Ctrl + Enter to run.
Get asked Palindrome Number in a real interview?
Interview Solver gives you a working solution live, during your actual coding interview. Invisible to screensharing.
How to solve Palindrome Number
Pattern: Reverse half the digits
x % 10 peels off the last digit and integer division by 10 drops it. Repeating that builds the number backwards, one digit at a time.
You don't need the whole reversal, only enough to compare. Keep moving digits from the end of x onto a reversed number until the reversed part is at least as large as what's left. At that point you've consumed half the digits. For an even digit count the two halves must be equal; for an odd count the middle digit ends up on the reversed side, so drop it with one more division before comparing.
Stopping at the halfway point also means the reversed value can never overflow, which matters in languages with fixed-width integers.
Approach 1: Convert to a string
Turn x into its decimal string and compare it with its reverse (or walk two indices inward). Short and correct, negatives fail naturally because of the -, but it uses extra memory and skips the arithmetic the question is usually after.
Time O(d) · Space O(d)
Approach 2: Reverse the whole number
Rebuild all of x backwards with % 10 and / 10, then compare with the original. Works, but in a 32-bit language the full reversal of a value like 2147483647 overflows.
Time O(d) · Space O(1)
Approach 3: Reverse only the second half
- Return false for negatives, and for any non-zero number ending in 0 (it would need a leading 0).
- While
x > reversed: appendx % 10toreversedand drop the last digit ofx. - Return
x == reversed(even length) orx == reversed / 10(odd length, the middle digit is ignored).
Time O(d) · Space O(1)
| Approach | Time | Space |
|---|---|---|
| Convert to a string | O(d) | O(d) |
| Reverse the whole number | O(d) | O(1) |
| Reverse only the second half | O(d) | O(1) |
Solution code
Try the practice editor first. Both solutions pass every test above.
Palindrome Number solution in Python
def is_palindrome(x: int) -> bool:
if x < 0 or (x % 10 == 0 and x != 0):
return False
reversed_half = 0
while x > reversed_half:
reversed_half = reversed_half * 10 + x % 10
x //= 10
return x == reversed_half or x == reversed_half // 10Palindrome Number solution in JavaScript
function isPalindrome(x) {
if (x < 0 || (x % 10 === 0 && x !== 0)) return false;
let reversedHalf = 0;
while (x > reversedHalf) {
reversedHalf = reversedHalf * 10 + (x % 10);
x = Math.floor(x / 10);
}
return x === reversedHalf || x === Math.floor(reversedHalf / 10);
}Common mistakes
- Forgetting the trailing-zero case. Without the early return, 10 becomes x = 0, reversed = 1 after one step, and `x == reversed / 10` wrongly reports true.
- Treating 0 as ending in zero and rejecting it. 0 is a palindrome.
- Using `/` in JavaScript without `Math.floor`, which produces fractions instead of dropping a digit.
- Reversing the full number in a fixed-width language and overflowing on large inputs.
What interviewers look for
Mention the string solution first and say it's fine for production code, then ask whether the interviewer wants the arithmetic version. Most do.
When you code the half reversal, trace one even-length number (1221) and one odd-length number (12321) out loud to show the two return conditions. Then name the two early exits (negatives, trailing zero) before you're asked about them.
Follow-up questions
Why does stopping at the halfway point avoid overflow?
The reversed half has at most half the digits of x, so it is far smaller than the maximum integer. A full reversal of a 10-digit 32-bit value can exceed 2³¹ - 1.
How would you check whether the number is a palindrome in binary?
Use the same idea with base 2: peel bits with `x & 1` and shift right, building the reversed bits with a left shift. Or compare bits from both ends using the position of the highest set bit.
How would you find the next palindrome larger than x?
Mirror the left half onto the right. If the result is larger than x, that's the answer; otherwise increment the left half (including the middle digit for odd lengths) and mirror again, handling the all-9s case, which rolls over to 10…01.
Companies that ask Palindrome Number
Palindrome Number is tagged by 118 companies in our interview dataset. The score shows how often it comes up at each company.
- Synology100%
- Applovin100%
- Luxoft100%
- Unanet95%
- Slack95%
- Postman94%
- Cognizant92.9%
- Snapchat91%
- Garmin89.1%
- Argyle87%
- Pornhub86.5%
- Canva85%
- Two Six Technologies84%
- Alnylam Pharmaceuticals84%
- Innovaccer84%
- Segment82%
- LTI80.7%
- TCS80%
- Pagerduty80%
- Sourcegraph80%
- Booz Allen Hamilton80%
- Convoy80%
- Accenture79.6%
- Deezer79%
and 94 more.
Practice next
Get live help in the real interview
Working solutions in real time during your actual interview. Invisible to screensharing.
Start Free TrialNo credit card required