AlgoMaster Logo

Palindrome Number

Last Updated: March 28, 2026

easy
4 min read

Understanding the Problem

We need to check whether an integer reads the same forwards and backwards. At first, this feels like a string problem in disguise: convert the number to a string and check if the string is a palindrome. That works, but the follow-up explicitly asks us to avoid string conversion.

So the real challenge is: how do you reverse or compare digits of a number using only arithmetic? The key observation is that you can extract digits from the end of a number using modulo and division, and you can build a reversed number digit by digit. If the reversed number equals the original, it's a palindrome.

There's also a quick shortcut worth noticing: negative numbers are never palindromes (the minus sign doesn't appear at the end), and numbers ending in 0 can't be palindromes unless the number itself is 0.

Key Constraints:

  • -2^31 <= x <= 2^31 - 1 → The input is a single 32-bit integer. No array to iterate over, so we're looking at O(log n) or O(1) approaches where n is the value of x (the number of digits is proportional to log10(x)).
  • The follow-up asks for a non-string solution, hinting that the interviewer values a mathematical approach.

Approach 1: String Conversion

Intuition

The most natural approach: convert the integer to a string and check if the string reads the same forwards and backwards. This is what most people would try first, and it's perfectly valid in a real interview as a starting point.

You can either reverse the string and compare, or use two pointers from both ends moving inward. Both are straightforward.

Algorithm

  1. Convert the integer to its string representation.
  2. Use two pointers: one at the start, one at the end.
  3. Compare characters at both pointers. If they ever differ, return false.
  4. Move the pointers inward and repeat until they meet.
  5. If all characters matched, return true.

Example Walkthrough

1Initialize: convert x=121 to string "121", left=0, right=2
0
1
left
1
2
2
1
right
1/3

Code

This approach works but allocates O(d) extra memory for the string. What if we could reverse the digits using only math, avoiding the string allocation entirely?

Approach 2: Full Number Reversal

Intuition

Instead of converting to a string, we can reverse the entire number mathematically. Extract digits from the end one at a time using x % 10, and build the reversed number by multiplying the accumulator by 10 and adding each digit. If the reversed number equals the original, it's a palindrome.

The one thing to watch out for: negative numbers always have the minus sign at the front, which can't appear at the back, so they're never palindromes. Handle that upfront.

Algorithm

  1. If x is negative, return false immediately.
  2. Store the original value of x.
  3. Initialize reversed to 0.
  4. While x is greater than 0, extract the last digit (x % 10), append it to reversed (reversed * 10 + digit), and remove the last digit from x (x / 10).
  5. Compare reversed with the original value. If equal, it's a palindrome.

Example Walkthrough

1Initialize: x=1221, original=1221, reversed=0
1221
1/6
1reversed starts at 0
0
1/6

Code

This works with O(1) space, but we reverse the entire number when we only need half. Reversing the full number also risks integer overflow for values near INT_MAX. What if we only reversed half the digits?

Approach 3: Reverse Half the Number

Intuition

Instead of reversing the whole number, only reverse the second half and compare it with the first half. For 1221, reverse the last two digits to get 12, and the remaining first half is also 12. They match, so it's a palindrome.

The trick is knowing when to stop. We keep extracting digits from the end until the reversed half is greater than or equal to the remaining number. At that point, we've processed exactly half the digits (or just past half for odd-length numbers).

This approach has two advantages over full reversal: it does half the work, and it completely avoids integer overflow since the reversed half never exceeds the original number.

Algorithm

  1. Handle special cases: if x is negative, return false. If x ends in 0 but isn't 0, return false (a number like 10 can't be a palindrome since no number starts with 0).
  2. Initialize reversedHalf to 0.
  3. While x is greater than reversedHalf, extract the last digit of x and append it to reversedHalf.
  4. After the loop, check:
    • For even-length numbers: x == reversedHalf
    • For odd-length numbers: x == reversedHalf / 10 (the middle digit doesn't need to match anything)

Example Walkthrough

1Initialize: x=1221, reversedHalf=0
1221
1/4
1reversedHalf starts at 0
0
1/4

Code