AlgoMaster Logo

Reverse Integer

mediumFrequency5 min readUpdated June 23, 2026

Understanding the Problem

We need to take an integer, reverse its digits, and return the result. The complication is overflow. The reversed number might not fit in a 32-bit signed integer, and if it does not, we return 0.

The constraint against 64-bit integers is what makes this problem more than a string exercise. Without that restriction, we could reverse the digits using a long, check if the result fits in 32 bits, and be done. The problem instead requires detecting overflow before it happens, using only 32-bit arithmetic.

Two properties of the input shape the solution. Negative numbers stay negative after reversal. Trailing zeros in the input (the 0 in 120) disappear when the number is reversed, because leading zeros are not represented in integers.

Key Constraints:

  • -2^31 <= x <= 2^31 - 1 → The input is a 32-bit signed integer, range [-2,147,483,648, 2,147,483,647]. The range is not symmetric: the minimum is one larger in magnitude than the maximum, so abs(Integer.MIN_VALUE) does not fit in an int.
  • 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

Convert the number to a string, reverse the string, and convert it back to an integer. This works, but it sidesteps the arithmetic and overflow logic the problem is built around, and it leans on string parsing to detect overflow rather than reasoning about it.

It serves as a baseline. We handle the sign separately, reverse the digit characters, then parse the result. Overflow detection comes from catching the exception that parsing throws (in Java, NumberFormatException) or comparing against the 32-bit 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 next approach reverses the digits with arithmetic alone and detects overflow before it happens, using only 32-bit comparisons.

Approach 2: Mathematical Digit-by-Digit Reversal

Intuition

Extract digits one at a time from the right (using x % 10) and build the reversed number from the left (by multiplying the running result by 10 and adding the digit). This is the standard technique for digit manipulation.

The part that needs care is overflow detection. Before computing result = result * 10 + digit, we check whether that operation would exceed 32-bit bounds. If result > Integer.MAX_VALUE / 10, then multiplying by 10 overflows regardless of the digit. If result == Integer.MAX_VALUE / 10 (which is 214,748,364), the digit decides it: any digit above 7 pushes the value past 2,147,483,647. The same reasoning applies on the negative side with -8, since Integer.MIN_VALUE ends in -8.

Algorithm

  1. Initialize result = 0.
  2. While x is not zero:
    • Extract the last digit: digit = x % 10.
    • Remove the last digit from x: x = x / 10.
    • 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.
    • Update result: result = result * 10 + digit.
  3. 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