Palindrome Number
An easy Math problem included in Love Babbar 450. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Math
- Sheets
- 1
- Core for
- 13 roles
- Platform
- LeetCode
The problem
Given an integer x, return true if x is a palindrome integer, and false otherwise. An integer is a palindrome when it reads the same forward and backward. Note that negative numbers are never palindromes.
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-09A negative sign can never appear at the end of an integer, so all negative values are disqualified immediately. Furthermore, reversing only the back half of the digits until it meets or exceeds the front half 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.
What each operation costs
| Operation | Time |
|---|---|
| greatest common divisor via euclidean algorithm | O(log(min(a, b))) |
| modular exponentiation by repeated squaring | O(log n) |
| prime sieve of eratosthenes up to n | O(n log log n) |
What usually goes wrong with Math
- Allowing 32-bit signed integer overflow during intermediate multiplications before modulo reduction is applied, producing negative or truncated results.
- Failing to handle negative inputs in modulo arithmetic, producing negative remainders in languages that implement truncated rather than floored division.
- Dividing by zero when the denominator or remainder becomes zero at unexpected edge boundary values like empty sets or singletons.
Which roles need this problem
Math is a core topic for these 13 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 7 more roles, including Security Engineer, Performance Engineer, Site Reliability Engineer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Math problems
Problem set and role mapping as of .