Palindrome Number

Easy
3.

Palindrome Number

Palindrome Number is an easy coding problem on math, asked in Morgan Stanley, Capital One, HCL and Mindtree interviews.

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?

Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number} x
 * @return {boolean}
 */
var isPalindrome = function(x) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

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.

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

  1. Hint 1

    Every negative number fails immediately — the minus sign never matches.

  2. 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

  1. Return false for x < 0.
  2. Build rev by repeatedly taking the low digit of a copy of x and appending it: rev = rev * 10 + n % 10.
  3. 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 == 0 holds.

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 == x

Java

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.

All 337 math problems · the whole catalogue