AlgoMaster Logo

Palindrome Number

easyFrequency7 min readUpdated June 23, 2026

Understanding the Problem

We need to check whether an integer reads the same forwards and backwards. The string approach is direct: convert the number to a string and check if the string is a palindrome. That works, but the follow-up asks us to avoid string conversion.

The arithmetic alternative reverses or compares the digits of a number using only math. You can extract digits from the end of a number with modulo and division, and build a reversed number digit by digit. If the reversed number equals the original, the number is a palindrome.

Two cases let us answer immediately without any reversal. Negative numbers are never palindromes, because the minus sign sits at the front and has no counterpart at the back. Numbers ending in 0 are never palindromes either, unless the number is 0 itself, because a palindrome's first digit would then have to be 0, and no integer is written with a leading zero.

Key Constraints:

  • -2^31 <= x <= 2^31 - 1 → The input is a single 32-bit integer. There is no array to iterate over, so the relevant cost is the number of digits, which is proportional to log10(x). This rules out anything more expensive than a single pass over the digits.
  • Reversing the full number can exceed the 32-bit range. For example, reversing 1463847412 produces 2147483641, and reversing some valid inputs overflows entirely. Any full-reversal solution must use a wider integer type or avoid the overflow another way.

Approach 1: String Conversion

Intuition

Convert the integer to a string and check if the string reads the same forwards and backwards. Place one pointer at the start and one at the end, compare the characters they point to, and move both inward. If any pair differs, the number is not a palindrome.

This works for negative inputs without a special case: the minus sign at index 0 never matches a digit at the end, so the comparison fails on the first step.

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 allocates O(d) extra memory for the string and ignores the follow-up's request to avoid string conversion. The next approach reverses the digits with arithmetic and removes the allocation.

Approach 2: Full Number Reversal

Intuition

Reverse the entire number with arithmetic instead of converting to a string. The expression x % 10 returns the last digit, and x / 10 removes it. Repeating this reads the digits from right to left. Each new digit is appended to a reversed accumulator with reversed = reversed * 10 + digit, where the multiply by 10 shifts the digits already collected one place left. When the original and reversed values match, every digit equals its mirror, which is the definition of a palindrome.

Negative numbers are handled with one check up front: the minus sign sits at the front and never appears at the back, so they return false before the loop runs.

Because reversing the full number can exceed the 32-bit range, the accumulator uses a 64-bit type in the implementations below.

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

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

Code

This runs in O(1) space, but it reverses the entire number when comparing half against half would suffice, and it needs a wider integer type to hold the full reversal. The next approach reverses only the second half of the digits, which does half the work and stays within the 32-bit range.

Approach 3: Reverse Half the Number

Intuition

Reverse only the second half of the digits and compare it with the first half. For 1221, the reversed last two digits give 12, and the remaining first half is also 12. They match, so the number is a palindrome.

The stopping point is the second half. Keep extracting digits from the end of x into reversedHalf until reversedHalf is greater than or equal to what remains in x. At that point exactly half the digits have moved across (one past half for odd-length numbers).

This has two advantages over full reversal: it processes half the digits, and reversedHalf never exceeds the original number, so no value can overflow a 32-bit integer.

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

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

Code