13. Palindrome Number
easyAsked at MonzoDecide whether an integer reference number reads the same forward and backward.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given an integer x, return true if x is a palindrome integer. An integer is a palindrome when it reads the same backward as forward. Negative numbers are not palindromes.
Constraints
-2^31 <= x <= 2^31 - 1Follow-up: solve without converting to string
Examples
Example 1
x = 121trueExample 2
x = -121falseApproaches
1. String reverse
Convert to a string and compare with its reverse.
- Time
- O(d)
- Space
- O(d)
const s = String(x);
return s === s.split('').reverse().join('');Tradeoff:
2. Half-reverse integer
Reverse only the lower half of the number and compare against the upper half. Avoids string allocation.
- Time
- O(d)
- Space
- O(1)
function isPalindrome(x) {
if (x < 0 || (x % 10 === 0 && x !== 0)) return false;
let rev = 0;
while (x > rev) {
rev = rev * 10 + x % 10;
x = Math.floor(x / 10);
}
return x === rev || x === Math.floor(rev / 10);
}Tradeoff:
Monzo-specific tips
Monzo likes seeing edge cases enumerated up front (negatives, trailing zeros) before any reversal logic.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Palindrome Number and other Monzo interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →