Reverse Integer

Medium
149.

Reverse Integer

Reverse Integer is a medium coding problem on math, asked in Amazon, Microsoft, Adobe and TCS interviews.

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to fall outside the signed 32-bit range [-2^31, 2^31 - 1], return 0 instead.

Assume the environment does not allow you to store 64-bit integers.

Example 1

Input
x = 123
Output
321

Example 2

Input
x = -123
Output
-321

Example 3

Input
x = 120
Output
21
Explanation
Trailing zeros of the input become leading zeros, which are dropped.

Constraints

  • -2^31 <= x <= 2^31 - 1
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 {number}
 */
var reverse = function(x) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Reverse Integer — Solution in Python, Java & C++

Reverse Integer solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem on math.

Reverse Integer is a medium coding problem on math, asked in Amazon, Microsoft, Adobe and TCS interviews.

  • Difficulty: Medium
  • Topics: Math
  • Asked at: Amazon, Microsoft, Adobe, TCS, Tech Mahindra
  • 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 a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to fall outside the signed 32-bit range [-2^31, 2^31 - 1], return 0 instead.

Assume the environment does not allow you to store 64-bit integers.

Example 1

Input: x = 123
Output: 321

Example 2

Input: x = -123
Output: -321

Example 3

Input: x = 120
Output: 21
Explanation: Trailing zeros of the input become leading zeros, which are dropped.

Constraints

  • -2^31 <= x <= 2^31 - 1

Hints for Reverse Integer

  1. Hint 1

    Peel digits off the right with x % 10 and push them onto a running result with result = result * 10 + digit.

  2. Hint 2

    Handle the sign once, up front, and reverse the absolute value.

  3. Hint 3

    Check the 32-bit bound after building the result — the overflow cases must return 0, not a wrapped value.

How to solve Reverse Integer

Peel digits off the right of |x| and push them onto the left of a running result — result = result * 10 + digit — then restore the sign and reject anything outside the 32-bit range.

Approach

  1. Record the sign and work with the absolute value.
  2. While digits remain, take n % 10, append it with result = result * 10 + digit, and drop it with n //= 10.
  3. Reapply the sign.
  4. Return 0 if the result lies outside [-2^31, 2^31 - 1], otherwise the result.

Why it works

Multiplying by ten and adding the next low digit is exactly what reversing the decimal string does, one place value at a time. Handling the sign separately means the digit loop only ever sees a non-negative value, so % and // behave the same in every language.

Complexity

  • Time — O(log x)
  • Space — O(1)

Pitfalls

  • In a language with fixed-width ints, check result > (INT_MAX - digit) / 10 before the multiply-add — a wrapped value that happens to land in range is still wrong.
  • The reversal of a number ending in zeros is shorter than the input — 120 becomes 21, which the arithmetic handles naturally.
  • -2147483648 reversed overflows even though its magnitude fits as input.

Reference solution

Python

def reverse(x: int) -> int:
    sign = -1 if x < 0 else 1
    n = abs(x)
    result = 0
    while n > 0:
        result = result * 10 + n % 10
        n //= 10
    result *= sign
    if result < -2147483648 or result > 2147483647:
        return 0
    return result

Java

public static int reverse(int x) {
    int sign = x < 0 ? -1 : 1;
    long n = Math.abs((long) x);
    long result = 0;
    while (n > 0) {
        result = result * 10 + n % 10;
        n /= 10;
    }
    result *= sign;
    if (result < Integer.MIN_VALUE || result > Integer.MAX_VALUE) return 0;
    return (int) result;
}

C++

int reverse(int x) {
    int sign = x < 0 ? -1 : 1;
    long long n = (long long) x;
    if (n < 0) n = -n;
    long long result = 0;
    while (n > 0) {
        result = result * 10 + n % 10;
        n /= 10;
    }
    result *= sign;
    if (result < -2147483648LL || result > 2147483647LL) return 0;
    return (int) result;
}

JavaScript

var reverse = function(x) {
    const sign = x < 0 ? -1 : 1;
    let n = Math.abs(x);
    let result = 0;
    while (n > 0) {
        result = result * 10 + (n % 10);
        n = Math.floor(n / 10);
    }
    result *= sign;
    if (result < -2147483648 || result > 2147483647) return 0;
    return result;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 337 math problems · the whole catalogue