Reversing the digits of an integer is one of the most common coding interview puzzles, popular on platforms like LeetCode. At first glance, the task seems simple: continuously extract the last digit using modulo arithmetic and append it to a running result. However, in real-world systems constrained by fixed-size data registers, the math gets tricky. When restricted to standard 32-bit signed integers—which span the range [-2,147,483,648, 2,147,483,647]—reversing a completely valid number can push its reversed sequence beyond these bounds.
In languages like Java, this overflow doesn't crash the program; instead, it silently wraps around, turning a massive positive value into a negative number and corrupting downstream calculations. In this guide, we will walk through the mathematical logic of reversing digits and explain how to detect and prevent integer overflow before it occurs.
To visualize this limitation, think of a mechanical car odometer with only 5 display digits (spanning from 00000 to 99999):
- Odometer Rollover: If you drive a car with
99990miles on the dash for an extra 15 miles, the physical gear wheels cannot display100005. Instead, the gears spin past the limit and reset to00005. - The Binary Equivalent: In computer registers, a similar rollover occurs. If you reverse the valid integer
1,534,236,469, the reversed digits mathematically equal9,646,324,351. Because this number exceeds the maximum limit of2,147,483,647, the binary representation overflows, yielding a corrupted negative value.
1. The Reversal Logic
The iterative approach to reversing a number involves peeling off the units digit one by one. By calculating the modulo ten of the input, we isolate the last digit, add it to our accumulated result (multiplied by ten), and then perform integer division by ten to discard it:
int lastDigit = num % 10;
reversedResult = (reversedResult * 10) + lastDigit;
num = num / 10; // Discard last digit
2. Guarding Against Overflow
Before running reversedResult * 10, we check if the operation will exceed Integer.MAX_VALUE (2,147,483,647) or fall below Integer.MIN_VALUE (-2,147,483,648) when divided by 10. The proactive guard checks if the current accumulated value is already past this threshold:
if (ans <= Integer.MIN_VALUE / 10 || ans >= Integer.MAX_VALUE / 10) {
return 0; // Return 0 as specified by LeetCode constraint
}
This prevents the overflow from ever happening, maintaining clean mathematical bounds.
Implementation in Java
Below is the complete solution in Java, demonstrating how to loop through the digits safely:
package io.practise.leetcode.medium;
public class ReversingInteger {
public static void main(String[] args) {
int num = 123678;
int ans = 0;
for (; num > 0; num = num / 10) {
// Check overflow limits before multiplying by 10
if (ans <= Integer.MIN_VALUE / 10 || ans >= Integer.MAX_VALUE / 10) {
ans = 0;
break;
}
int temp = num % 10;
ans = (ans * 10) + temp;
}
System.out.println("Reversed: " + ans); // Reversed: 876321
}
}
Conclusion
Handling numerical constraints is the difference between writing scripts and engineering robust software. By checking register limits against Integer.MAX_VALUE / 10 before executing multiplication, you ensure that your code remains safe from silent numeric corruption.