AlgoMaster Logo

Reverse Integer

Last Updated: March 28, 2026

medium
3 min read

Understanding the Problem

We need to take an integer, reverse its digits, and return the result. Sounds simple enough, but there's a twist: we have to watch out for overflow. The reversed number might not fit in a 32-bit signed integer, and if it doesn't, we return 0.

The constraint about not using 64-bit integers is what makes this problem interesting. Without that restriction, you could just reverse the digits using a long, check if the result fits in 32 bits, and call it a day. But the problem wants us to detect overflow before it happens, using only 32-bit arithmetic. That's the real challenge here.

A few things to notice: negative numbers should stay negative after reversal. Trailing zeros in the input (like the 0 in 120) naturally disappear when the number is reversed, since leading zeros aren't represented in integers.

Key Constraints:

  • -2^31 <= x <= 2^31 - 1 → The input is a standard 32-bit signed integer. The range is [-2,147,483,648, 2,147,483,647]. Notice the range isn't symmetric: the minimum is one larger in magnitude than the maximum.
  • No 64-bit integers → We cannot use long in Java or int64 in Go to sidestep overflow detection. We must check for overflow within 32-bit bounds.

Approach 1: String Reversal

Intuition

The most straightforward way to reverse digits is to convert the number to a string, reverse the string, and convert it back to an integer. This is what most people think of first, and it works, but it's not the approach interviewers are looking for. It sidesteps the math and overflow logic that the problem is designed to test.

Still, it's worth understanding as a baseline. We handle the sign separately, reverse just the digit characters, then parse the result. For overflow detection, we can catch the exception that parsing throws (in Java, NumberFormatException) or compare against bounds.

Algorithm

  1. Record whether the number is negative, then work with the absolute value.
  2. Convert the absolute value to a string.
  3. Reverse the string.
  4. Parse the reversed string back to an integer.
  5. If parsing would overflow 32-bit bounds, return 0.
  6. Restore the sign and return.

Example Walkthrough

1x = -123, sign = negative. Convert abs(123) to string: "123"
0
1
1
2
2
3
1/4

Code

The string conversion isn't slow, but it violates the spirit of the problem. Can we reverse the digits purely with arithmetic and detect overflow before it happens using only 32-bit comparisons?

Approach 2: Mathematical Digit-by-Digit Reversal

Intuition

Instead of converting to a string, we can extract digits one at a time from the right (using x % 10) and build the reversed number from the left (by multiplying our running result by 10 and adding the digit). This is the core technique for any digit manipulation problem.

The key insight is overflow detection. Before we do result = result * 10 + digit, we need to check whether this operation would overflow. If result > Integer.MAX_VALUE / 10, then multiplying by 10 will definitely overflow. If result == Integer.MAX_VALUE / 10, then adding any digit greater than 7 would push us past 2,147,483,647. The same logic applies on the negative side with -8.

This approach is what interviewers expect. It demonstrates understanding of integer representation, modular arithmetic, and overflow handling.

Algorithm

  1. Initialize result = 0.
  2. While x is not zero:

a. Extract the last digit: digit = x % 10.

b. Remove the last digit from x: x = x / 10.

c. Check for overflow before updating result:

  • If result > MAX_VALUE / 10 or (result == MAX_VALUE / 10 and digit > 7), return 0.
  • If result < MIN_VALUE / 10 or (result == MIN_VALUE / 10 and digit < -8), return 0.

d. Update result: result = result * 10 + digit.

  1. Return result.

Example Walkthrough

1x = 1534236469, result = 0. Extract digits from right.
0
1
1
5
2
3
3
4
4
2
5
3
6
6
7
4
8
6
9
9
extract
1/7

Code