MediumMathUpdated Sep 2026

7. Reverse Integer

Asked at Google, Amazon, Meta, Bloomberg

Difficulty

Medium

Time

O(log x)

Space

O(1)

Pattern

Math

Companies

4

Problem

You are given a signed 32-bit integer x. Return a new integer made from the digits of x in reverse order, keeping the same sign.

Leading zeros disappear naturally: reversing 120 gives 21, not 021. The number 0 reverses to 0.

The tricky part is overflow. The answer must still fit in a signed 32-bit integer, meaning the range [-2^31, 2^31 - 1]. If the reversed value would fall outside that range, return 0 instead. You may not use 64-bit integers to detect this — you must check before each step that builds the result.

Reverse the digits, keep the sign

x = 123123012The rightmost digit 3 is the first one we take.result321012Each new digit is appended on the right, so the numberreads 321.
For x = 123 we pull digits off the right end and push them onto the left of the answer, giving 321.

Constraints

  • ●-2^31 <= x <= 2^31 - 1
  • ●The reversed value must also fit in the signed 32-bit range, otherwise return 0
  • ●You cannot use 64-bit integers to hold the result

Examples

Example 1
Inputx = 123
Output321
WhyThe digits read right to left are 3, 2, 1, so the reversed number is 321. It fits in the 32-bit range, so we return it.
Example 2
Inputx = -123
Output-321
WhyThe sign stays with the number. Reversing the digits of 123 gives 321, and we keep the minus sign, so the answer is -321.
Example 3
Inputx = 120
Output21
WhyReading 120 backwards gives 021, but a leading zero is not a real digit, so the value is just 21.

Asked At

4 companies

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

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

Pop a digit, check overflow, push it

digit = x % 10check result * 10 + digit fitsresult = result * 10 + digitx = x / 10 (drop last digit)Repeat these four steps until x becomes 0.x = 123startstep 1step 2step 3x1231210digit321—result332321321After three steps x is empty and result holds 321,which is inside the 32-bit range.INT_MAX = 2147483647overflow if result > 214748364or result == 214748364 and digit > 7on overflow: return 0
We peel digits off x with % 10 and build the answer with result = result * 10 + digit, checking the range before each push.
ComplexityTime O(log x)Space O(1)The loop runs once per digit, and the number of digits grows like log base 10 of x; only a few integer variables are stored.

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

O(log x) time
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 runs in O(log x) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Reverse Integer?

Reverse Integer is asked at Google, Amazon, Meta, Bloomberg. 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.