This problem is popular in MAANG (Meta, Microsoft, Amazon, Google, etc.) coding interviews because it tests more than basic digit manipulation. It checks whether you can work with numbers directly, extract digits using math operations, and handle edge cases and overflow safely while building the answer.
Problem statement
You are given a signed 32-bit integer, x. Your task is to return x with its digits reversed.
- If reversing
xcauses the resultant value to go beyond the signed 32-bit integer range, [-231, 231 – 1], return 0. - Assume the environment does not support storing 64-bit integers, so you must handle overflow carefully.
Constraints:
- -231 <=
x<= 231 – 1
Examples
| Input | Output | Explanation |
| 7462 | 2647 | Reverse the digits of a positive number. |
| -5809 | -9085 | Reverse digits and keep the negative sign. |
| 30040 | 4003 | Trailing zeros drop after reversing (04003 → 4003). |
| 5 | 5 | Single-digit numbers remain the same when reversed. |
| 0 | 0 | Reversing zero is still zero. |
| 1534236469 | 0 | Reversed value would exceed 2^31 – 1, so return 0 (overflow). |
| -1563847412 | 0 | Reversed value would be less than -2^31, so return 0 (underflow). |
| 1463847412 | 2147483641 | Reversal stays within 32-bit signed range, so it’s valid. |
Key observations
We need to reverse the digits of an integer (for example, 7462 → 2647). However, there are two extra rules we must follow: If the number is negative, the reversed result should remain negative. If the reversed number goes outside the 32-bit signed integer range, we must return 0.
One shortcut is to convert the number to a string and reverse it, but that avoids the real integer logic and does not help with practicing overflow-safe arithmetic. Instead, we reverse the number the same way you would do it by hand:
- We repeatedly take the last digit of the number.
- We append that digit to the end of a new number that we are building as the reversed result.
Optimized approach: Reverse digits safely without strings
We use a digit-by-digit approach because it mirrors how reversal works in basic arithmetic and lets us control overflow without relying on string tricks.
Handling the sign:
We first determine whether the input is negative or positive. We store that sign and then work with the absolute value, so digit operations stay straightforward.
Building the reversed number:
We start the reversed result at zero and process the number one digit at a time. In each step, we take the last digit from the remaining number, remove it from the remaining number, and then add it to the end of the result. The last digit is taken using modulo 10, and the remaining number is shortened using integer division by 10. To attach the digit, we shift the current result one place left (multiply by 10) and then add the digit. This is why reversing 7462 builds up as 2, then 26, then 264, and finally 2647.
Preventing overflow:
After adding each digit, we check whether the number we are building has gone beyond the 32-bit signed integer limit. If it has, we return 0 right away.
Returning the result:
Once the remaining number becomes zero, all digits have been processed. We then apply the saved sign to the reversed result and return it. This keeps negative numbers negative and naturally drops any trailing zeros from the original number after reversal.
Step-by-step algorithm
- Set
limit = 2**31 - 1to represent the maximum allowed 32-bit signed integer value. Next, initialize the reversed number asresult = 0. - Find the sign of
x:- If
xis negative, setsign = -1. - Otherwise, set
sign = 1.
- If
- Convert
xto a positive number for easier digit handling by settingnum = abs(x). - While
numstill has digits, i.e., it is not zero:- Use
num, digit = divmod(num, 10)to get the last digit indigit, and shortennum. - Update
result = result * 10 + digitto add that digit to the end of the reversed number. - If
result > limit, return0because it is too large for 32-bit range.
- Use
- When
numchanges to 0, return the signed result usingsign * result.
The slides below help to visualize the solution steps in a better way.
Python code for the optimized solution
Here is the Python implementation of the optimized solution we discussed above.
Code for the Reverse Integer problem
Time complexity
The time complexity of this solution is O(log10(∣x∣)) because each loop iteration removes one digit by doing num //= 10, and the number of times you can divide by 10 before reaching 0 is the number of digits, which is about log10(∣x∣)+1.
Space complexity
The space complexity of this solution is constant, O(1), as it uses only a few fixed variables.
Common mistakes to avoid
Here are the common mistakes to keep in mind for the Reverse Integer problem:
- Integer overflow: The most common mistake is failing to check if the number exceeds the 32-bit signed integer range ([-231, 231 – 1]). Always check for potential overflow before multiplying the current result by 10.
- The “Last Digit” trick: Use the mathematical approach (pop = x % 10; rev = rev * 10 + pop) instead of converting the integer to a string. String conversion is slower and often seen as a “cheat” in technical interviews.
- Handling trailing zeros: Numbers like 120 should reverse to 21. If you use the mathematical approach, the leading zero is handled automatically, but if you use strings, you must manually strip them.