Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
Example 1:
Input: x = 123 Output: 321
Example 2:
Input: x = -123 Output: -321
Example 3:
Input: x = 120 Output: 21
Constraints:
-231 <= x <= 231 - 1The key idea in Reverse Integer is to rebuild the number digit by digit in reverse order. You repeatedly extract the last digit of the integer using x % 10, then append it to a new number that represents the reversed value. After extracting the digit, remove it from the original number using integer division x / 10.
While rebuilding the reversed number, you must carefully handle the 32-bit signed integer range (-2^31 to 2^31 - 1). Before multiplying the current result by 10 and adding the next digit, check whether the operation would cause an overflow. If it would exceed the allowed range, return 0.
This approach avoids converting the number to a string and works purely with arithmetic operations, making it efficient and interview-friendly. Since each digit is processed once, the algorithm runs in O(log10 n) time with O(1) extra space.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Digit-by-digit reversal with overflow check | O(log n) | O(1) |
CodeHelp - by Babbar
This method involves extracting digits one at a time from the integer using modulus and division operations. By repeatedly taking the last digit of the number, you can construct the reversed integer. However, special care must be taken to handle the overflow condition, as reversing an integer can potentially produce a value beyond the 32-bit signed integer limit.
Time Complexity: O(log10(x)) because we are processing each digit once.
Space Complexity: O(1) since we are using constant extra space.
1#include <stdio.h>
2#include <limits.h>
3
4int reverse(int x) {
5 int result = 0;
6
This C solution uses a while loop to pop digits from the input number x and push them onto the result. Overflow conditions are checked both before multiplication and addition.
In this approach, the integer is first converted to a string representation, reversed as a string, and then converted back to an integer. This method mitigates overflow issues by checking the integer post-conversion if it falls within the valid range.
Time Complexity: O(n) where n is the number of digits in x.
Space Complexity: O(n) due to the use of the string to store digits temporarily.
1def reverse(x):
2 sign = -
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, Reverse Integer is a common interview question because it tests number manipulation, edge case handling, and overflow awareness. Variations of this problem are frequently used in technical interviews at top tech companies.
No special data structure is required for Reverse Integer. The most efficient solution uses simple arithmetic operations with integers, keeping only a few variables to track the current digit and reversed result.
The optimal approach extracts digits using modulo and rebuilds the number in reverse using multiplication and addition. During each step, you check whether multiplying by 10 would cause a 32-bit overflow. This ensures the solution runs efficiently without using extra data structures.
Before updating the reversed number, check whether multiplying the current result by 10 would exceed the 32-bit signed integer limits. If the result would go beyond the allowed range, return 0 immediately. This prevents undefined integer overflow behavior.
This Python solution converts the integer x to a string, reverses it, and then converts it back to an integer. The sign is preserved and checked for overflow before returning the result.