Medium
Math
Updated Sep 2026

Reverse Integer

Asked at Google, Amazon, Meta

Problem

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

Asked At

How to Think About It

1.

Extract digits from right to right using modulo 10. Build the reversed number by multiplying the result by 10 and adding the extracted digit.

2.

Overflow check: before multiplying by 10, check if the result would exceed INT_MAX or go below INT_MIN. Specifically: if result > INT_MAX // 10 or (result == INT_MAX // 10 and digit > 7), it overflows.

3.

Why 7: INT_MAX = 2,147,483,647. The last digit is 7. So if result == 214748364 and digit > 7, it overflows. Similarly for INT_MIN = -2,147,483,648, last digit is 8.

4.

Visual walkthrough for x=123:
digit=3, result=010+3=3. No overflow.
digit=2, result=3
10+2=32. No overflow.
digit=1, result=32*10+1=321. No overflow.
x=0. Return 321.

5.

Visual walkthrough for x=-123:
digit=-3, result=010+(-3)=-3.
digit=-2, result=-3
10+(-2)=-32.
digit=-1, result=-32*10+(-1)=-321.
Return -321.

6.

Edge cases: x=0 (return 0), INT_MAX (check overflow), numbers ending in 0 (120 -> 21).

Optimal Approach

Step 1: Initialize result = 0.
Step 2: While x != 0:

  • Extract digit = x % 10
  • Check overflow: if result > INT_MAX // 10 or (result == INT_MAX // 10 and digit > 7), return 0
  • Update result = result * 10 + digit
  • Update x = x // 10 (integer division toward zero)
    Step 3: Return result.

Time: O(log x) where log is base 10 (number of digits). Space: O(1).

What Trips People Up in Real Interviews

1.

Not checking for overflow before updating result. The check must happen before result = result * 10 + digit. Checking after the multiplication is too late, as the overflow has already occurred and the value is corrupted.

2.

Using string conversion (str(x)[::-1]) instead of arithmetic. While this works for small numbers, it fails the interview because it does not demonstrate understanding of digit extraction with modulo and integer division, and it does not naturally integrate the overflow check.

3.

Getting the overflow boundary wrong for negative numbers. INT_MIN is -2,147,483,648 (last digit 8), not -2,147,483,647. Checking against digit > 7 for negative numbers misses the -8 edge case and allows overflow.

4.

Using x % 10 incorrectly for negative numbers in Python. Python modulo returns non-negative results for positive divisors, so -123 % 10 gives 7, not -3. Use int(x / 10) for division toward zero and x % 10 only after accounting for the sign, or extract digit = x % 10 then adjust.

5.

Integer overflow when result is near INT_MAX. Before multiplying by 10, check result > INT_MAX // 10. If result equals INT_MAX // 10, then the digit must be <= 7. Skipping this two-part check allows values like 2,147,483,648 to sneak through.

Solution Code

def reverse(x):
    result = 0
    while x != 0:
        digit = x % 10
        x = int(x / 10)
        if result > 2**31 // 10 or (result == 2**31 // 10 and digit > 7):
            return 0
        if result < -(2**31) // 10 or (result == -(2**31) // 10 and digit < -8):
            return 0
        result = result * 10 + digit
    return result

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Reverse Integer problem?

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

How do you solve Reverse Integer?

The optimal approach is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.

What companies ask Reverse Integer?

Reverse Integer is asked at Google, Amazon, Meta. It is a medium difficulty problem.

What are common mistakes on Reverse Integer?
  • Not checking for overflow before updating `result`. The check must happen before `result = result * 10 + digit`. Checking after the multiplication is too late, as the overflow has already occurred and the value is corrupted.
  • Using string conversion (`str(x)[::-1]`) instead of arithmetic. While this works for small numbers, it fails the interview because it does not demonstrate understanding of digit extraction with `modulo` and integer division, and it does not naturally integrate the overflow check.
  • Getting the overflow boundary wrong for negative numbers. `INT_MIN` is -2,147,483,648 (last digit 8), not -2,147,483,647. Checking against `digit > 7` for negative numbers misses the -8 edge case and allows overflow.
  • Using `x % 10` incorrectly for negative numbers in Python. Python modulo returns non-negative results for positive divisors, so `-123 % 10` gives 7, not -3. Use `int(x / 10)` for division toward zero and `x % 10` only after accounting for the sign, or extract `digit = x % 10` then adjust.
  • Integer overflow when `result` is near `INT_MAX`. Before multiplying by 10, check `result > INT_MAX // 10`. If `result` equals `INT_MAX // 10`, then the digit must be <= 7. Skipping this two-part check allows values like 2,147,483,648 to sneak through.