Magicsheet logo

Palindrome Number

Easy
85.8%
Updated 6/1/2025

Palindrome Number

What is this problem about?

The Palindrome Number problem asks whether an integer x is a palindrome — reads the same forwards and backwards. Negative numbers are never palindromes. This easy problem tests integer manipulation without converting to a string, or string-based digit reversal. The math interview pattern is the core.

Why is this asked in interviews?

J.P. Morgan, Apple, Microsoft, Meta, Amazon, Google, Bloomberg, and virtually every tech company ask this easy problem. It's one of the first problems candidates encounter. The string conversion approach is trivial; the follow-up (without using a string) requires integer digit reversal and comparison.

Algorithmic pattern used

Reverse half the integer. To avoid overflow, reverse only the second half. While x > reversed_half: take the last digit (x % 10), add to reversed_half * 10. Divide x by 10. x is palindrome if x == reversed_half (even length) or x == reversed_half // 10 (odd length, middle digit in reversed_half).

Example explanation

x=12321. reverse until x ≤ reversed_half:

  • reversed_half=1, x=1232.
  • reversed_half=12, x=123.
  • reversed_half=123, x=12. Now x(12) < reversed_half(123). x == reversed_half//10 → 12 == 12 ✓. Return true.

x=12331: similarly reverse: reversed_half=133, x=12. 12 ≠ 133//10=13. Return false.

Common mistakes candidates make

  • Not handling negatives (always false).
  • Not handling x ending in 0 (except x=0 itself, no palindrome can end in 0).
  • Overflow risk if reversing the full integer.
  • Off-by-one: for even-length numbers, x == reversed_half; for odd, x == reversed_half // 10.

Interview preparation tip

Palindrome Number tests two things: edge case awareness (negatives, trailing zeros) and integer manipulation. The "reverse half" technique avoids overflow and is O(log n). Always handle edge cases first: if x < 0 or (x % 10 == 0 and x != 0), return false. Practice this problem with the follow-up constraint "without converting to string" to demonstrate mathematical thinking over string shortcuts.

Similar Questions