Skip to content

Practice Palindrome Number

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#9
LeetCode Problem

Palindrome Number

Problem Statement

Given an integer x, return true if x is a palindrome, and false otherwise.

Source: Palindrome Number on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
x = 121
Output
true

# Example 2

Input
x = -121
Output
false

# Example 3

Input
x = 10
Output
false

Constraints

-2^31 <= x <= 2^31 - 1

How to solve Palindrome Number

Original notes for interview preparation, written for this problem. Reviewed .

Key insight

Reversing only the lower half of the digits and comparing it with the untouched upper half avoids overflow entirely, and the loop can stop exactly when the reversed part grows to at least the size of what remains.

Why brute force is not enough

Convert the number to a string and compare it with its reverse. It is O(d) time and extra space for d digits and works, but the follow-up asks to avoid string conversion, and fully reversing the integer instead risks overflow (2147483647 reversed does not fit in 32 bits).

Approach

  1. Return false for negative numbers, since the minus sign has no mirror, and for positive numbers ending in zero other than zero itself, since a palindrome cannot start with zero.
  2. Build a reversed value by repeatedly peeling the last digit off x and appending it, continuing only while x is strictly greater than the reversed value. The invariant is that reversed holds the mirrored lower digits and x holds the remaining upper digits.
  3. When the loop ends, x and reversed have the same number of digits (even total) or x has one extra digit, the middle one (odd total).
  4. Return true if x equals reversed, or if x equals reversed with its last digit removed.

Walkthrough of Example 1

x = 121 → true

  1. 121 is non-negative and does not end in 0, so proceed with reversed = 0.
  2. Peel 1: reversed becomes 1 and x becomes 12. Since 12 > 1, continue.
  3. Peel 2: reversed becomes 12 and x becomes 1. Now 1 is not greater than 12, so stop.
  4. x (1) does not equal reversed (12), but it equals reversed with the middle digit dropped, 12 / 10 = 1. Return true.

Time and space

Time O(log10 x) · Space O(1). Roughly half of the number's decimal digits are peeled, each in constant time; only two integers are stored.

Interview pitfalls

  • Skipping the trailing-zero check: for x = 10 the loop produces reversed 1 and x 0, and the odd-length comparison 0 equals 1 / 10 wrongly returns true.
  • Looping while x is greater than or equal to reversed instead of strictly greater, which over-peels even-length numbers like 1221 and returns false.
  • Reversing the whole number in 32-bit arithmetic; inputs near 2^31 - 1 produce reversed values that overflow.
  • Treating negatives by their magnitude, so -121 wrongly returns true; the sign is part of the number.
  • Single-digit inputs, including 0, must return true; verify that the final comparison still succeeds when the loop runs once (x becomes 0 and reversed holds the digit) or not at all.

Follow-up questions interviewers ask

  • How would you test for a palindrome in binary or another base?
  • How would you count palindromic numbers in a large range without testing each candidate?
  • How would you find the smallest palindrome greater than a given number?

Further reading on the technique: Palindromic number

Related LeetCode Problems

Practice more problems similar to Palindrome Number.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.