Quick revision
The short answer
We return false at once for negative numbers and for numbers that end in 0 other than 0 itself. Then we reverse only the second half of the digits: while x > rev, we move the last digit of x onto rev. When the loop stops, the number is a palindrome if x == rev (even digit count) or x == rev / 10 (odd digit count, where the middle digit is dropped). It takes O(log10 n) time, one step per digit, O(1) space, and it cannot overflow because rev never grows beyond about half the digits.
What to remember
In this answer, we checked whether an int is a palindrome without strings. We returned false early for negative numbers and for numbers ending in 0 other than 0, then reversed only half of the digits with rev = rev * 10 + x % 10 while x > rev, and compared x == rev for even lengths or x == rev / 10 for odd lengths. Reversing only half keeps rev small, so it never overflows, unlike a full reversal of values near Integer.MAX_VALUE. The method runs in O(log10 n) time and O(1) space.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We return false at once for negative numbers and for numbers that end in 0 other than 0 itself. Then we reverse only the second half of the digits: while x > rev, we move the last digit of x onto rev. When the loop stops, the number is a palindrome if x == rev (even digit count) or x == rev / 10 (odd digit count, where the middle digit is dropped). It takes O(log10 n) time, one step per digit, O(1) space, and it cannot overflow because rev never grows beyond about half the digits.
Why this question is asked
Checking whether a number reads the same backwards is easy with strings: convert, reverse, compare. Interviewers forbid the string to see whether we can work with digits arithmetically, and whether we notice the overflow trap that the obvious arithmetic solution falls into. It is also a nice test of edge cases, because negatives, trailing zeros and odd digit counts each need thought. In this answer, we will recap how to take digits off a number, see why reversing the whole number is risky, build the half-reversal solution step by step, trace it on both odd and even lengths, state the complexity, and finish with how to test the boundaries.
1A Quick Recap of Digit Arithmetic
Two operations take an integer apart from the right. x % 10 gives the last digit, and x / 10 removes it, because integer division in Java truncates. Building a number from digits works the other way: rev * 10 + d appends digit d to the end of rev. With those three operations we can reverse a number one digit at a time without any strings.
2The Obvious Approach and Its Overflow Trap
The first idea is to reverse the entire number and compare it with the original. It is correct in logic, but the reversed number may not fit in an int. Integer.MAX_VALUE is 2,147,483,647. A valid input such as 1,999,999,999 reversed is 9,999,999,991, which is far beyond the range, so the multiplication silently wraps around and produces garbage.
A real palindrome reversed is the number itself, so it never overflows, and the naive version usually gives the right answer. But for other inputs it compares against a wrapped, meaningless value, which is fragile reasoning, and the same pattern in a 'reverse an integer' method returns plainly wrong results. Using a long for rev fixes it, but the elegant solution avoids the issue entirely by reversing only half.
3Reversing Only Half of the Digits
A palindrome's second half, reversed, equals its first half. So we keep taking digits off the end of x and appending them to rev, and stop as soon as rev is no longer smaller than x. At that point we have consumed half the digits, or half plus the middle one. For an even number of digits, the halves are equal: x == rev. For an odd number, rev has the middle digit as its last digit, so we drop it with rev / 10 and compare.
Two early checks make the loop condition safe. Negative numbers are never palindromes, because of the minus sign. And a number ending in 0 can only be a palindrome if it is 0 itself, since no number starts with 0. Without this check, 10 would go through the loop twice: the first step gives x = 1 and rev = 0, the second gives x = 0 and rev = 1, and then x == rev / 10 compares 0 with 0 and wrongly returns true. So the trailing zero check is not optional.
Notice why overflow is impossible here. The loop stops when rev reaches x, and by then rev holds at most about half the digits of the original, plus one. For an int of 10 digits, rev holds at most 5 or 6 digits, which is nowhere near 2 billion. So the method is correct for every int without using long.
4Tracing the Loop on Odd and Even Lengths
Following the two variables shows exactly where the loop stops and why the two comparisons are needed.
| Input | Steps (x, rev) | Stops at | Result |
|---|---|---|---|
| 1221 | (122, 1) -> (12, 12) | x = 12, rev = 12 | x == rev: true |
| 12321 | (1232, 1) -> (123, 12) -> (12, 123) | x = 12, rev = 123 | x == rev / 10: true |
| 123 | (12, 3) -> (1, 32) | x = 1, rev = 32 | 1 != 32 and 1 != 3: false |
| 10 | rejected early | trailing 0 | false |
5Complexity
Each loop iteration removes one digit from x, and we stop after about half the digits, so the number of iterations is about half of log10(x) + 1, which is O(log10 n) or simply O(d) for d digits. An int has at most 10 digits, so in practice the loop runs at most 5 times. Space is O(1): two int variables. The string version is also O(d) in time, but it allocates a String and, for the reversal, a StringBuilder, so the arithmetic version avoids garbage on a hot path and avoids the overflow concerns of a full reversal.
6Follow-up questions
How would you test the boundary values?
I would write a parameterised JUnit test with the examples 121, -121 and 10, then the edge cases: 0 and single digits 1 to 9 (true), 11 and 1001 (true, even lengths), 12321 (odd length), 100 and 1000 (trailing zeros, false), Integer.MAX_VALUE 2147483647 (false), the largest int palindrome 2147447412 (true), Integer.MIN_VALUE (false, negative). For extra confidence I would add a property test comparing the method with the string version, new StringBuilder(s).reverse(), on many random ints including values near the limits.
Why is the comparison x > rev and not x != 0?
x != 0 reverses all the digits, which is the full reversal with its overflow risk. x > rev stops exactly at the halfway point, which is what keeps rev small and lets us compare the two halves.
Would the same approach work for long?
Yes, with long variables. The half reversal still cannot overflow, because rev holds at most about 10 of the 19 digits of a long.
7Conclusion
In this answer, we checked whether an int is a palindrome without strings. We returned false early for negative numbers and for numbers ending in 0 other than 0, then reversed only half of the digits with rev = rev * 10 + x % 10 while x > rev, and compared x == rev for even lengths or x == rev / 10 for odd lengths. Reversing only half keeps rev small, so it never overflows, unlike a full reversal of values near Integer.MAX_VALUE. The method runs in O(log10 n) time and O(1) space.
Open with
I would first handle the edge cases. A negative number is never a palindrome because of the sign, and a number that ends in zero can only be a palindrome if it is zero itself, so I return false for both.
Then, if they want more
Then, instead of reversing the whole number, I reverse only half of it. In a loop that runs while x is greater than rev, I take the last digit with x mod 10, append it to rev with rev times ten plus the digit, and remove it from x with x divided by ten. The loop stops at the middle.
If the number has an even number of digits, the two halves are equal, so x equals rev. If it has an odd number, the middle digit ends up at the end of rev, so I compare x with rev divided by ten.
For 12321, the loop ends with x as 12 and rev as 123, and 12 equals 123 over 10. The reason for reversing only half is overflow: reversing a large int like 1,999,999,999 gives a number above Integer.MAX_VALUE, which wraps silently, while half of the digits always fits easily.
The time is O(log10 n), one step per digit and at most five iterations for an int, and the space is constant. For testing, I would cover zero, single digits, even and odd lengths, trailing zeros, negative values and the extremes like Integer.MAX_VALUE.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a Java method boolean isPalindrome(int x) that checks whether an integer reads the...": https://scrivio-interview-library.netlify.app/java/coding-problems/efe703fea6/