Browse curriculum

Reverse Integer

Reverse the digits of a 32-bit integer by popping the last digit and pushing it onto the result, checking for overflow before each push.

Problem Understanding

Reverse Integer: given a signed 32-bit integer x, return x with its digits reversed. If the reversed value falls outside [-2^31, 2^31 - 1], return 0. Assume you cannot store 64-bit integers.

Examples: 123 → 321, -123 → -321, 120 → 21.

The Intuition: Pop From One End, Push Onto the Other

Two arithmetic moves do the whole job:

  1. Pop the last digit of x: digit = x % 10, then x /= 10.
  2. Push it onto the end of result: result = result * 10 + digit.

Repeat until x is 0 and the digits come out reversed. The only hard part is the 64-bit ban: result * 10 can overflow before you get a chance to compare it. So compare first. INT_MAX is 2147483647, so if result is already above 214748364, or equal to it with a next digit above 7, the push would overflow. The same digit-at-a-time arithmetic underlies Pow(x, n), which reads the bits of n instead of decimal digits, and Plus One.

Interactive Walkthrough

The top row is what is left of x; the bottom row is result, with the digit just pushed highlighted. Try Overflow to see the check stop the run, and Just fits for a reverse that lands 6 below the limit.

Dry Run Table

Input: x = 120

Step digit x after result
1 0 12 0
2 2 1 2
3 1 0 21

Answer: 21. The trailing zero is pushed first, while result is still 0, so it leaves no trace.

The Solution Template

Java, C++ and C check both bounds before the push. Python reverses the absolute value and restores the sign. JavaScript numbers are exact well past 2^31, so it can push first and compare after.

Reverse Integer Code
1var reverse = function(x) {
2 const MAX = 2 ** 31 - 1, MIN = -(2 ** 31);
3 let result = 0;
4 while (x !== 0) {
5 const digit = x % 10;
6 x = Math.trunc(x / 10);
7 result = result * 10 + digit;
8 if (result > MAX || result < MIN) return 0;
9 }
10 return result;
11};

Edge Cases & Common Mistakes

  • Checking after the multiply: in C, C++ and Java, result * 10 has already overflowed (undefined behaviour in C and C++), so the check comes too late.
  • INT_MIN: -2147483648 has no positive counterpart; taking abs of it overflows in fixed-width languages, so keep the sign in each digit instead.
  • Trailing zeros: 120 becomes 21, not 021.
  • Zero: the loop never runs and the answer is 0.
  • Using a 64-bit long and comparing at the end: works in practice, but breaks the stated constraint.

Summary

Pop the last digit with % 10 and / 10, push it onto result with * 10 +, and check against INT_MAX / 10 and INT_MIN / 10 before every push. Return 0 on overflow. O(log |x|) time, O(1) space.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Pop a digit

    digit = x % 10, then x /= 10 (truncating toward zero).

  2. Check before pushing

    If result * 10 + digit would leave the 32-bit range (compare result with INT_MAX / 10 and INT_MIN / 10), return 0.

  3. Push the digit

    result = result * 10 + digit. Repeat until x is 0, then return result.

Frequently asked questions

How do you detect overflow in Reverse Integer without 64-bit integers?

Check before the multiply. result * 10 + digit overflows when result > INT_MAX / 10, or result == INT_MAX / 10 and digit > 7 (INT_MAX ends in 7). On the negative side it is result < INT_MIN / 10, or equal with digit < -8 (INT_MIN ends in 8).


How are negative numbers handled?

In Java, C++, C and JavaScript, x % 10 keeps the sign of x, so every digit of a negative number is negative and result builds up as a negative number on its own. In Python, % is always non-negative, so the usual trick is to reverse abs(x) and put the sign back.


What happens to trailing zeros?

They become leading zeros of the result, which disappear: 120 reverses to 21. Popping a 0 just multiplies result by 10 while it is still 0.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description