Scrivio Interview library
Math and bitsMath and bit manipulationReported 20 times · 13 min read · updated 14 November 2026

Check whether a number is a palindrome.

1What makes a number a palindrome, seen on a folded strip of paper

Write a number on a strip of paper, one digit per square, and fold the strip in half. If every digit lands on top of an equal digit, the number reads the same from both ends, and we call it a palindrome. The word comes from the same idea in language, like the word level, which reads the same backwards. Our whole method will be a way of folding the number in half using arithmetic alone.

Take 12321. The first digit is 1 and the last is 1. The second is 2 and the second to last is 2. The middle 3 folds onto itself. Every pair agrees, so 12321 is a palindrome. 1221 is one too, with an even number of digits and no middle. 123 is not, because reversed it reads 321. Every single digit number, 0 included, is a palindrome, since it has nothing to disagree with.

PictureThe digits of 12321, compared from both ends

A number is a palindrome when its first and last digits agree, then its second and second to last, and so on to the middle.

  1. 1021322314firstlast

    1The outer pair is 1 and 1, the next pair is 2 and 2, and the middle 3 has no partner. Every pair agrees, so 12321 is a palindrome.

What to assume

What type is the input?
We assume a Java int, which may be negative and may be as large as 2,147,483,647.
Is -121 a palindrome?
Read backwards it is 121-, so no, and no negative number is.
What about 10?
Reversed it would be 01, which is not how 10 is written, so no.
May we turn the number into a string?
We assume the interviewer asks us not to, which is the usual follow-up.

This question looks like a string problem in disguise, and it is smaller than it looks. A Java int has at most ten digits, so whatever we do, we touch at most ten digits. The real test is in the details: negative numbers, numbers ending in zero, and numbers so large that reversing them does not fit.

A careful candidate asks a few questions. What type is the input? We assume a Java int, which may be negative and may be as large as 2,147,483,647. Is -121 a palindrome? Read backwards it is 121-, so no, and no negative number is. What about 10? Reversed it would be 01, which is not how 10 is written, so no. May we turn the number into a string? We will try that first, then assume the interviewer asks us not to, which is the usual follow-up.

2How to spot a digit arithmetic pattern

A beginner's first thought is usually to look at the first and last digits, as we did on paper. With a string that is easy, because we can jump to any position. With a number it is not. There is no direct way to ask an int for its first digit, so the paper method does not carry over as it is.

The first clue is the word number. The question is about the digits of an integer, and that points to two small operations every programmer should know. x % 10, the remainder after dividing by 10, gives the last digit: 12321 % 10 is 1. x / 10, whole-number division, drops the last digit: 12321 / 10 is 1232. Used together in a loop, they peel the digits off one at a time from the right.

The second clue is reading from both ends. We cannot reach the first digit cheaply, but we can build numbers from the back. The operation value * 10 + digit adds a digit to the end of a number we are building: 12 * 10 + 3 is 123. So we can peel digits off the end of x and stack them onto a new number, which builds the reverse of whatever we peel.

These clues belong to the math and bits family, and the technique is digit arithmetic. The name only means working with the digits of a number through % and /, never through text. The rule to carry to new problems is that whenever a question is about the digits of an integer, a loop of x % 10 and x / 10 walks them from right to left in one step per digit, and value * 10 + digit builds a number from them. The same loop reverses an integer, sums its digits, and checks for numbers whose digits have special properties.

Some patterns look close but do not fit. Two pointers moving in from both ends are the natural method for a string palindrome, and the idea behind our check is the same comparison of outer pairs, but on a number we cannot place a pointer on the first digit. Bit manipulation also belongs to this family, but it works with binary digits, and the question is about decimal ones.

3The honest first attempt, a brute force through strings

The honest first attempt turns the number into text, where we already know how to check a palindrome, and compares the text with its reverse. It is short, it is correct, and it is the right thing to say first in an interview, before improving it.

Step throughThe string attempt on -121

Converting to a string keeps the minus sign, so the reversed string can never equal the original for a negative number.

  1. -0112213

    1Integer.toString(-121) gives the four characters -, 1, 2, 1. The minus sign is the first character.

  2. 102112-3

    2Reversed, the string reads 1, 2, 1, -. The first characters already differ, so equals returns false, the right answer.

Java
static boolean isPalindromeByString(int x) {
String s = Integer.toString(x); // "-121" keeps its minus sign
return new StringBuilder(s).reverse().toString().equals(s);
}

Does it handle the awkward cases? For -121 the string keeps its minus sign, so the reverse, 121-, differs and the answer is false, which is right. For 10 the reverse is 01, which differs from 10, also right. For 12321 the reverse is identical, so it returns true.

Let us count its cost. A number n has about log10(n) + 1 digits, d for short, so 12321 has 5. Building the string, reversing it and comparing each take about d steps, so the time is O(d), which is O(log n). The space is also O(log n), for the two strings. The cost is small, so where is the waste? The question is about arithmetic, yet we create objects, and an interviewer usually asks us to do it without strings.

The next idea is to reverse the whole number with arithmetic and compare the result with the original. That avoids strings, but it has a trap. The largest int, 2,147,483,647, reversed is 7,463,847,412, which does not fit in an int. Java does not warn us; the value wraps round to -1,126,087,180. So a full reversal can overflow, and it also does twice the work we need. Once the back half is reversed, it can be compared with the front half directly.

4From the full reversal to folding the number in half

So we reverse only the second half, which is exactly the paper fold. We keep a number called reversedHalf, starting at 0. In each step we take the last digit of x with x % 10, add it to the end of reversedHalf with reversedHalf * 10 + digit, and drop it from x with x / 10. Each step moves one digit from the end of x onto the end of reversedHalf.

When do we stop? A beginner might count the digits first to know where the middle is. We do not need to. As digits move across, x shrinks and reversedHalf grows, so we stop as soon as x is no longer larger than reversedHalf. For a palindrome, at that moment half the digits have moved, or half plus one when the count is odd. For other numbers the loop may run a step longer, which does no harm, because the comparison then fails.

Now we compare. With an even number of digits, the front half left in x must equal the reversed back half, as in 1221, which ends with x = 12 and reversedHalf = 12. With an odd number of digits, the middle digit has moved across too and sits at the end of reversedHalf, so we drop it with reversedHalf / 10 before comparing. For 12321 the loop ends with x = 12 and reversedHalf = 123, and 123 / 10 is 12. And because reversedHalf never holds more than about half the digits, it can never overflow.

Two kinds of input need an answer before the loop. Negative numbers return false, because of the minus sign. Numbers ending in 0 also return false, except 0 itself, because a palindrome ending in 0 would have to start with 0, and no number is written that way. Is that check really needed, or is it only caution? Let us run 10 through the loop without it and see.

Step throughWhy a trailing zero needs its own check

The number 10 run through the loop without the early check. The zero that should lead the reversed half cannot be stored.

  1. 1001
    x
    10
    reversedHalf
    0

    1Suppose we skip the early check and give the loop x = 10. It is larger than reversedHalf = 0, so the loop starts.

  2. 1001
    x
    1
    reversedHalf
    0

    2The last digit is 0, so reversedHalf stays 0 * 10 + 0 = 0, and x becomes 1. x is still larger, so we go on.

  3. 1001
    x
    0
    reversedHalf
    1

    3The digit 1 moves across: reversedHalf becomes 1 and x becomes 0. The leading zero of the reversed half has silently vanished.

  4. 1001
    x
    0
    reversedHalf
    1

    4Now x == reversedHalf / 10 asks whether 0 == 1 / 10, and 1 / 10 is 0 in whole numbers, so the check wrongly says 10 is a palindrome.

That is the trap. A zero at the front of a number has no value, so when 0 is moved onto an empty reversedHalf it simply disappears, and the comparison is fooled. The early check catches every such number before the loop starts. This is the mistake a beginner is most likely to make, because the loop looks complete without it.

5Watching the fold on 12321 and 1221

The picture plays the real loop on both examples. The cells are the digits of the number, and a marked digit is one that has been moved from the end of x onto reversedHalf. The panel shows x, reversedHalf and the loop test after each step.

Watch the two numbers in the panel. One shrinks and the other grows, and the loop stops at the step where they cross.

Step throughReversing half of 12321 and then of 1221

The cells show the digits of the number. Marked digits have been moved from the end of x onto reversedHalf.

  1. 1021322314
    number
    12321
    x
    12321
    reversedHalf
    0
    test
    x > reversedHalf, go on

    1We start with x = 12321 and reversedHalf = 0. x is larger than reversedHalf, so the loop begins.

  2. 1021322314
    number
    12321
    x
    1232
    reversedHalf
    1
    test
    x > reversedHalf, go on

    2x % 10 is 1, the last digit. reversedHalf becomes 0 * 10 + 1 = 1, and x / 10 drops that digit, leaving 1232.

  3. 1021322314
    number
    12321
    x
    123
    reversedHalf
    12
    test
    x > reversedHalf, go on

    3The next last digit is 2. reversedHalf becomes 1 * 10 + 2 = 12 and x becomes 123. x is still larger, so we go on.

  4. 1021322314
    number
    12321
    x
    12
    reversedHalf
    123
    test
    stop the loop

    4The digit 3 moves across: reversedHalf becomes 123 and x becomes 12. Now x is not larger than reversedHalf, so the loop stops.

  5. 1021322314
    number
    12321
    x
    12
    reversedHalf
    123
    test
    x == reversedHalf / 10

    5The digit count is odd, so the middle 3 sits at the end of reversedHalf. 123 / 10 is 12, which equals x, so 12321 is a palindrome.

  6. 10212213
    number
    1221
    x
    1221
    reversedHalf
    0
    test
    x > reversedHalf, go on

    6Now an even number. x = 1221 and reversedHalf = 0, so the loop begins again.

  7. 10212213
    number
    1221
    x
    122
    reversedHalf
    1
    test
    x > reversedHalf, go on

    7The last digit, 1, moves across: reversedHalf is 1 and x is 122. x is still larger.

  8. 10212213
    number
    1221
    x
    12
    reversedHalf
    12
    test
    stop the loop

    8The digit 2 moves across: reversedHalf is 12 and x is 12. x is no longer larger, so the loop stops after two steps.

  9. 10212213
    number
    1221
    x
    12
    reversedHalf
    12
    test
    x == reversedHalf

    9With an even digit count the two halves must be equal, and x == reversedHalf is 12 == 12, so 1221 is a palindrome too.

For 12321, the three steps move 1, then 2, then 3, so reversedHalf grows 1, 12, 123 while x shrinks 1232, 123, 12. After the third step 12 is not larger than 123, so the loop ends. The count of digits was odd, so the middle 3 is greyed out: 123 / 10 drops it, and 12 equals 12.

For 1221 only two steps are needed. After the second, x and reversedHalf are both 12, the loop test fails, and x == reversedHalf is true. Each number needed only half of its digits to be moved, which is the saving over the full reversal.

6The complete Java program

The method handles the two early cases, runs the half-reversal loop, and compares for both even and odd digit counts. main runs both examples, a number that is not a palindrome, a negative number, a trailing zero, zero itself, and the largest int.

Java
public class PalindromeNumber {
// Reverse only the second half of the digits and compare it with the first half.
static boolean isPalindrome(int x) {
if (x < 0 || (x % 10 == 0 && x != 0)) return false; // minus sign, or a trailing zero
int reversedHalf = 0;
while (x > reversedHalf) {
reversedHalf = reversedHalf * 10 + x % 10; // move the last digit across
x /= 10;
}
// Even digit count: halves are equal. Odd: drop the middle digit from reversedHalf.
return x == reversedHalf || x == reversedHalf / 10;
}
public static void main(String[] args) {
System.out.println(isPalindrome(12321)); // true
System.out.println(isPalindrome(1221)); // true
System.out.println(isPalindrome(123)); // false
System.out.println(isPalindrome(-121)); // false
System.out.println(isPalindrome(10)); // false
System.out.println(isPalindrome(0)); // true
System.out.println(isPalindrome(Integer.MAX_VALUE)); // false
}
}

Let us read isPalindrome in the order it runs. The first line returns false for a negative x and for any x that ends in 0 but is not 0. The part x != 0 lets 0 itself through, since 0 is a palindrome.

reversedHalf starts at 0. The loop runs while x > reversedHalf. In each pass, x % 10 is the last digit, reversedHalf * 10 + x % 10 adds it to the end of reversedHalf, and x /= 10 drops it from x. The two lines must run in that order; if we divided x first, we would lose the digit before moving it.

The return line covers both shapes at once. x == reversedHalf is the even case. x == reversedHalf / 10 is the odd case, where the middle digit is dropped. We do not need to know which shape we have. Trying both is safe, because when the digits do not pair up from both ends, neither test can hold, as 123 shows below.

So main prints true for 12321 and 1221, false for 123, false for -121, false for 10, true for 0, and false for 2147483647. The largest int needs only five steps, ending with x = 21474 and reversedHalf = 74638, so nothing overflows.

7How much it costs, and the cases at the edges

Count on the example first. 12321 has five digits and the loop ran three times; 1221 has four and the loop ran twice. A number n has about log10(n) + 1 digits, and the loop moves about half of them, so the time is O(log n). For an int that is never more than six steps. We keep two ints whatever the input, so the space is O(1).

Comparing the approaches
ApproachTimeExtra spaceOverflow risk
Convert to a string and reverseO(log n)O(log n)none
Reverse the whole numberO(log n)O(1)yes, unless we use long
Reverse only the second halfO(log n)O(1)none

The edge cases each have a clear path through the code. Negative numbers return false at once. Numbers ending in 0 return false at once, except 0, which passes the check, skips the loop because 0 is not larger than 0, and returns true because 0 equals 0.

A single digit, such as 7, moves across in one step: reversedHalf becomes 7 and x becomes 0, and 0 equals 7 / 10, so the answer is true. Very large values never overflow, because reversedHalf holds at most about half the digits, and 123 shows a non-palindrome: the loop ends with x = 1 and reversedHalf = 32, and neither test holds.

8Follow-up questions

How would you reverse a whole integer safely, the related question?

We use the same loop over all the digits, but before each reversed * 10 + digit we ask whether it would overflow. If reversed is already greater than Integer.MAX_VALUE / 10, multiplying by 10 is too much; if it equals Integer.MAX_VALUE / 10, the digit may be at most 7, the last digit of 2,147,483,647. When the next step would overflow, we return 0 or throw, as the problem asks. Negative numbers work with the same loop, because Java's % keeps the sign, as long as the check uses Integer.MIN_VALUE for them.

Can you check a palindrome in another base, such as binary?

Yes. We replace 10 with the base. In binary, x % 2 gives the last bit and x / 2 drops it, or x & 1 and x >>> 1 do the same with bit operations. The half reversal works unchanged, and the trailing zero rule becomes a trailing zero bit, which means every even number other than 0 is not a binary palindrome.

9Conclusion

We checked whether a number is a palindrome without strings by folding it in half: x % 10 and x / 10 peel digits off the end, reversedHalf * 10 + digit stacks them in reverse, and the loop stops when x is no longer larger than reversedHalf. Equal halves, or equal after dropping the middle digit, mean a palindrome. Negative numbers and numbers ending in 0, apart from 0, are rejected first. It runs in O(log n) time and O(1) space and cannot overflow, unlike a full reversal. The clue to remember is that any question about the digits of a number is a loop of x % 10 and x / 10.

What to say in the interviewAbout 80 seconds aloud

Open with

First I would clarify that the input is an int, that negative numbers are not palindromes because of the minus sign, and whether I may use strings. The simplest way is to convert to a string and compare it with its reverse, which is linear in the number of digits but allocates strings.

Then, if they want more

Reversing the whole number with arithmetic avoids strings, but the reverse of a large int can overflow. So I reverse only half. Negative numbers return false, and so does anything ending in zero except zero itself, since no number starts with zero; without that check, 10 would wrongly pass.

Then, while x is greater than reversedHalf, I take x mod 10, append it to reversedHalf by multiplying by ten and adding, and divide x by ten. When the loop stops, about half the digits have moved.

If the digit count is even, the halves must be equal; if it is odd, reversedHalf has the middle digit at the end, so I compare x with reversedHalf divided by ten. For 12321 that ends with 12 and 123, and 12 equals 12. It is O(log n) time and constant space, and reversedHalf never gets big enough to overflow.

About this answer

Last updated
14 November 2026
Written for
Java 21, using only java.util
Checked
The Java solution was compiled and run, and each printed result matches the comment beside it. The pictures are checked against the format when the site is built. The explanation around the code is a study note: confirm anything critical against the documentation below. What the checks cover
Where the question comes from
Public interview write-ups, reworded by Scrivio; counted 20 times in that collection. How it is counted
Official documentation
java.util, package documentation
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 "Check whether a number is a palindrome.": https://scrivio-interview-library.netlify.app/data-structures/solutions/check-whether-a-number-is-a-palindrome-6d27ef/