Day 3 of 90EasyLeetCode

Palindrome Number

Decide whether an integer reads the same forwards and backwards, ideally without turning it into a string.

Pattern
Peel digits with % 10 and / 10
Time box
15 min
Time
O(log x)
Space
O(1)

Try it first — give it 15 minutes

0/4 revealed

Stuck? Take one hint at a time. The struggle is where the pattern sticks.

    The problem in plain English

    You get an integer x, which can be anywhere in the 32-bit range (about −2.1 billion to +2.1 billion). Return true if it reads the same from left to right as from right to left, and false otherwise.

    Examples: 121 → true. -121 → false, because backwards it reads 121-. 10 → false, because backwards it reads 01.

    Start with brute force

    Turn x into a string and compare it with its reverse: str(x) == str(x)[::-1]. It's correct and fast, but LeetCode's follow-up asks you to do it without a string, and interviewers often do too. Working with digits directly is a skill you'll reuse in Plus One.

    Building the intuition

    How do you get at the digits of a number without a string? Try it on 123:

    • 123 % 10 is 3. The remainder after dividing by 10 is always the last digit.
    • 123 / 10 is 12 in integer division. Dividing by 10 removes the last digit.

    Repeat and you get 3, 2, 1: the digits right to left, which is exactly the reversed order. Now build it up. Start with rev = 0, and for each digit do rev = rev * 10 + digit. Multiplying by 10 shifts everything one place left to make room for the new digit: 0 → 3 → 32 → 321. A palindrome is a number that equals its own reverse.

    The optimal approach

    1. If x < 0, return false. The minus sign can't appear at the end.
    2. Save original = x and set rev = 0 (short for "reversed").
    3. While x > 0: rev = rev * 10 + x % 10, then x = x / 10 (integer division).
    4. Return rev == original.

    The subtle detail is overflow. 2147483647 fits in an int, but its reverse, 7463847412, doesn't. In C++ and Java, store rev in a 64-bit long long / long. Python and JavaScript don't have this problem at this size.

    The code

    Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.

    class Solution:
        def isPalindrome(self, x: int) -> bool:
            if x < 0:                # "-121" backwards is "121-": never a palindrome
                return False
    
            original = x
            rev = 0                  # the digits of x, read right to left
            while x > 0:
                digit = x % 10       # last digit
                rev = rev * 10 + digit   # shift left, append the digit
                x //= 10             # chop the last digit off
    
            return rev == original

    Dry run

    x = 121

    x beforedigit = x % 10rev afterx after
    1211112
    122121
    111210

    x is now 0, so we stop. rev = 121 equals original = 121, so the answer is true.

    Complexity

    • Time: O(log x). We do one step per digit, and a number x has about log₁₀(x) digits. At most 10 steps here.
    • Space: O(1). Three integer variables.

    Edge cases and traps

    • Negative numbers. Always false. Check it explicitly rather than relying on the loop.
    • Zero. A palindrome: the loop doesn't run and 0 == 0.
    • Trailing zeros. 10 reverses to 1, so it's correctly false.
    • Overflow in C++/Java. An int reversed can exceed 2³¹ − 1. Use 64 bits.
    • / in Python and JavaScript. Python needs // for integer division. JavaScript needs Math.floor(x / 10).

    Say it in the interview

    "Negatives can't be palindromes, so I return false. Otherwise I reverse the number with maths: mod 10 gives the last digit, dividing by 10 removes it, and rev times 10 plus the digit appends it. Then I compare. O(log x) time, O(1) space, and a 64-bit reverse so it can't overflow."

    Follow-ups interviewers ask

    • "What if you can't use a 64-bit type?" Reverse only half the digits. Stop when rev >= x; then compare x == rev (even length) or x == rev / 10 (odd length, the middle digit doesn't matter). Half a number's digits can't overflow. Reject numbers ending in 0 first, except 0 itself.

    Practise the same idea

    This is Day 3 of a free 90-day plan 🐸

    90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.

    Get the full roadmap — free