Reverse Integer
Maintained by Vikas D, software engineer
You are handed one integer that fits inside a signed 32-bit slot, meaning it sits somewhere between -2^31 and 2^31 - 1. Your job is to flip the order of its digits and hand back the resulting number.
The sign stays put. A negative input produces a negative output, and any zeros that were trailing on the original simply vanish once they move to the front, since a number does not keep leading zeros.
There is one catch that makes this trickier than a plain string flip. The reversed value might be larger than what a 32-bit integer can hold. When that happens you must return 0 rather than a wrapped or truncated number.
An added rule forbids you from leaning on a wider 64-bit type to dodge the overflow. You have to detect that the answer would spill past the limit using only 32-bit-safe arithmetic, before the overflow actually occurs.
Examples
The digits 1, 2, 3 read backwards give 3, 2, 1, which is well inside range.
The minus sign is preserved and only the digit order flips.
Reversing gives 021, and the leading zero is dropped to leave 21.
The reversed value 9646324351 exceeds 2^31 - 1, so the function returns 0 instead.
Constraints
- -2^31 <= x <= 2^31 - 1 (the input fits in a signed 32-bit integer).
- The environment cannot store 64-bit integers, so overflow must be detected with 32-bit-safe checks.
- Return 0 whenever the reversed result would leave the signed 32-bit range.
Visible test cases
- reverseNumber(123) == 321
- reverseNumber(-123) == -321
- reverseNumber(120) == 21
- reverseNumber(0) == 0
- reverseNumber(1534236469) == 0
- reverseNumber(-2147483648) == 0
Hints
Hint 1
Pull the digits off one at a time with modulo 10 and integer division by 10. This lets you build the reversed number without converting to a string.
Hint 2
The sign takes care of itself if you keep the input's sign through the loop, or you can strip the sign up front and reattach it at the end.
Hint 3
Check for overflow BEFORE you do result = result * 10 + digit. Compare the running result against INT_MAX / 10 (and the symmetric bound for negatives) so you never actually compute a value too large to hold.
Approaches
String reversal with range check
Time O(d) where d is the number of digits · Space O(d) for the string bufferTreat the number as text, reverse the characters, parse it back to a number, then verify the result still fits in 32 bits.
- Record the sign and take the absolute value as a string.
- Reverse the string of digits and parse it back into a number.
- Reapply the sign.
- If the parsed value falls outside [-2^31, 2^31 - 1], return 0, otherwise return it.
Digit-by-digit math with pre-overflow guard
Time O(d) where d is the number of digits · Space O(1)Rebuild the reversed number with arithmetic only, and test against the 32-bit boundary before each multiply-and-add so overflow is caught without ever forming an out-of-range value.
- Keep a running result that starts at 0.
- On each step take the last digit with x % 10 and drop it from x with integer division by 10.
- Before doing result = result * 10 + digit, check whether result has already passed INT_MAX / 10, or equals it while the next digit would push past the final allowed digit. The same test mirrors for the negative bound.
- If either guard trips, return 0 immediately.
- When x reaches 0, return the accumulated result.
Common mistakes
- Computing the reversed value first and checking the range afterward defeats the no-64-bit rule. The overflow has to be caught before the multiply-and-add, not after.
- Forgetting that 2^31 - 1 ends in 7 and -2^31 ends in 8, so the last-digit tie-break bounds are asymmetric. A generic check that uses the same digit for both signs is subtly wrong.
- Mishandling negatives in languages where % can return a negative remainder versus those where it does not. Strip the sign first or account for the sign of the modulo in your guard.
- Assuming you must do something special for trailing zeros. Numeric reversal drops them automatically because numbers carry no leading zeros.
Follow-up variations
- How would the solution change if you were allowed a 64-bit accumulator? Which checks could you drop?
- What if the range were a signed 64-bit integer instead of 32-bit? How do you detect overflow then with no 128-bit type?
- Could you reverse the number in place without an extra accumulator variable?
- How would you reverse the digits of an arbitrary-length big integer represented as a digit array?