Palindrome Number — Solution in Python, Java & C++
Palindrome Number solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on math.
Palindrome Number is an easy coding problem on math, asked in Morgan Stanley, Capital One, HCL and Mindtree interviews.
- Difficulty: Easy
- Topics: Math
- Asked at: Morgan Stanley, Capital One, HCL, Mindtree, Mphasis, Virtusa
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given an integer x, return true if x is a palindrome, and false otherwise.
An integer is a palindrome when it reads the same forward and backward. For example, 121 is a palindrome while 123 is not.
Example 1
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
Example 2
Input: x = -121
Output: false
Explanation: From left to right it reads -121. From right to left it becomes 121-. Therefore it is not a palindrome.
Example 3
Input: x = 10
Output: false
Explanation: Reads 01 from right to left.
Constraints
-2^31 <= x <= 2^31 - 1
Follow-up: can you solve it without converting the integer to a string?
Hints for Palindrome Number
Hint 1
Every negative number fails immediately — the minus sign never matches.
Hint 2
Build the reversed number with digit math (rev = rev * 10 + x % 10) and compare it to the original.
How to solve Palindrome Number
Negatives are never palindromes; for everything else, reverse the number arithmetically and compare it with the original.
Approach
- Return
falseforx < 0. - Build
revby repeatedly taking the low digit of a copy ofxand appending it:rev = rev * 10 + n % 10. - Return
rev == x.
Why it works
A number reads the same backwards exactly when its digit sequence reversed produces the same value, and the multiply-add loop constructs that reversed value directly. Comparing integers avoids any string allocation.
Complexity
- Time —
O(log x) - Space —
O(1)
Pitfalls
- The fully reversed value of a 10-digit palindrome candidate can overflow a 32-bit int; reversing only the second half (stop when
rev >= n) avoids that entirely. - Zero is a palindrome — the loop runs zero times and
0 == 0holds.
Reference solution
Python
def isPalindrome(x: int) -> bool:
if x < 0:
return False
n, rev = x, 0
while n > 0:
rev = rev * 10 + n % 10
n //= 10
return rev == xJava
public static boolean isPalindrome(int x) {
if (x < 0) return false;
int n = x;
long rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
return rev == (long) x;
}C++
bool isPalindrome(int x) {
if (x < 0) return false;
int n = x;
long long rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
return rev == (long long) x;
}JavaScript
var isPalindrome = function(x) {
if (x < 0) return false;
let n = x, rev = 0;
while (n > 0) {
rev = rev * 10 + (n % 10);
n = Math.floor(n / 10);
}
return rev === x;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.