Palindrome Number
An easy Math interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Math
- Sheets
- 1
- Core for
- 13 roles
The problem
Decide whether the digits of the integer x read the same from left to right as from right to left. A negative number cannot pass, because its minus sign has no partner at the other end.
Example 1
- Input
- x = 121
- Output
- true
- Why
- 121 reads the same from left to right and right to left.
Example 2
- Input
- x = -121
- Output
- false
- Why
- Negative numbers are not palindromes because the minus sign would not appear at the end when reversed.
Example 3
- Input
- x = 10
- Output
- false
- Why
- 10 reversed is 01, which is not equal to 10.
Constraints
- -2^31 <= x <= 2^31 - 1
How to think about it
Updated 2026-09-29A negative sign can never appear at the end of an integer, so all negative values are disqualified immediately. Reversing only the back half of the digits until it meets or exceeds the front half also avoids 32-bit arithmetic overflow entirely.
Approaches, worst first
Convert to string and two pointers
time O(log10(x)) · space O(log10(x))
Format the integer into a string and compare characters inward from both ends. This is straightforward but incurs heap allocation and string formatting overhead.
Full integer reversal
time O(log10(x)) · space O(1)
Extract digits using modulo 10 and assemble a fully reversed integer, then check equality with the original. Reversing a 32-bit integer can overflow the signed integer limit unless handled with 64-bit storage.
Reversing half of the numberWrite this one
time O(log10(x)) · space O(1)
Peel digits off the tail into a reversed accumulator until the remaining front is less than or equal to the accumulator. Compare front with accumulator (or accumulator / 10 for odd lengths), completely eliminating overflow risk and saving half the steps.
Where people lose marks · 2
- Non-zero multiples of 10, such as 10 or 100, end in 0 but never start with 0; if not guarded early, half-reversal treats them incorrectly because remaining front and reversed back both become 0.
- Reversing the full integer can overflow a signed 32-bit integer when x is near 2^31 - 1.
The theory behind it
Math — the ground this problem stands on. All Math problems
What Math is
Algorithmic math is the application of number properties, modular arithmetic, and geometric patterns to compute results without simulating every step. Rather than filling memory buffers with millions of simulated counters or walking endless loops, mathematical formulation uses identities like prime factorization, digit extraction, and greatest common divisors to jump directly to target answers through closed-form rules.
When to reach for it
Reach for mathematical techniques when problem bounds are massive, such as constraints reaching ten to the eighteenth power where simulation is impossible. Strong prompts mention prime generation, greatest common divisor calculations, modular inverses, fast exponentiation, digit reversals, or combinatorial arrangements. If brute-force counting produces memory overflow or time limit exceeded warnings, algebraic reorganization is the intended path.
How the pattern works
Focus on numeric constraints and potential overflows before writing expressions. Break numbers into constituent digits using modulo ten to peel least significant figures and integer division to shrink scale. For divisibility, apply Euclid's remainder theorem repeatedly until remainder drops to zero. Apply modulo arithmetic at every intermediate addition and multiplication stage rather than once at the end, preventing values from spilling beyond standard numeric register limits.
More Math problems
Target Roles
Core requirement for 13 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .