L1 · FoundationsNumbers & loops~3 min · 6 tests

Check If a Number Is a Palindrome

Check whether an integer reads the same backwards, like 121, without converting it to a string. Negative numbers are never palindromes. Run tests live.

The problem

Return True if the integer n reads the same forwards and backwards. Negative numbers are not palindromes. Avoid str().

Examples

  1. Example 1

    Input

    is_palindrome_number(121)

    Expected output

    True
  2. Example 2

    Input

    is_palindrome_number(123)

    Expected output

    False

+ 4 hidden tests on Submit.

Edge cases to ask about

  • Negative numbers
  • Zero
  • Trailing zero (10)

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · is_palindrome_number
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and read why.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    str(n) == str(n)[::-1]O(d)O(d)
    bestReverse arithmetically and compareO(d)O(1)
    Walkthrough of the optimal approach (try it yourself first)

    Reverse the number with the % 10 / // 10 loop and compare with the original. Negative numbers fail immediately because the - has no mirror.

    Complexity: O(log n) time, O(1) space. Reversing touches each of the ~log₁₀(n) digits once.

    Reveal the reference solution
    def is_palindrome_number(n):
        if n < 0:
            return False
        original, rev = n, 0
        while n:
            rev = rev * 10 + n % 10
            n //= 10
        return rev == original

    Follow-ups interviewers ask

    • Reverse only half the digits to stop early.

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Check If a Number Is a Palindrome in Python?

    The optimal solution runs in O(log n) time and O(1) auxiliary space. Reversing touches each of the ~log₁₀(n) digits once.

    What is the brute-force approach, and how do you optimise it?

    str(n) == str(n)[::-1]: O(d) time, O(d) space. Reverse arithmetically and compare: O(d) time, O(1) space.

    What follow-up questions do interviewers ask about Check If a Number Is a Palindrome?

    Reverse only half the digits to stop early.