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 [-2^31, 2^31 - 1]
, then return 0
. Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
For example:
Write a function to implement this. Discuss the time and space complexity. How would you handle edge cases such as:
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy aims to find the reversed version of a number by meticulously examining each digit. It involves converting the number into a sequence of characters, reversing this sequence, and then converting it back to a number. Finally, it also includes a check to make sure that the reversed number remains within the allowed range.
Here's how the algorithm would work step-by-step:
def reverse_integer(number):
is_negative = number < 0
# If negative, store the information and work with the absolute value
if is_negative:
number = -number
number_string = str(number)
reversed_string = number_string[::-1]
try:
reversed_number = int(reversed_string)
except ValueError:
return 0
# Apply the sign that was previously removed
if is_negative:
reversed_number = -reversed_number
# Check for overflow before returning the result
if reversed_number < -2**31 or reversed_number > 2**31 - 1:
return 0
return reversed_number
To reverse an integer efficiently, we don't treat it as a normal number, but rather work on its digits one by one. The main trick is to extract the digits from the right and build the reversed number while carefully checking for potential overflow issues during the construction process.
Here's how the algorithm would work step-by-step:
def reverse_integer(number):
reversed_number = 0
INT_MAX = 2**31 - 1
INT_MIN = -2**31
while number != 0:
# Extract the last digit from the original number
digit = number % 10
number = int(number / 10)
# Check for overflow before multiplying
if reversed_number > INT_MAX // 10 or (reversed_number == INT_MAX // 10 and digit > 7):
return 0
# Check for underflow before multiplying
if reversed_number < INT_MIN // 10 or (reversed_number == INT_MIN // 10 and digit < -8):
return 0
# Build the reversed number
reversed_number = reversed_number * 10 + digit
return reversed_number
Case | How to Handle |
---|---|
Positive integer with trailing zeros like 1200 | The reversed integer will naturally drop trailing zeros; the code should not add them back. |
Negative integer like -123 | Store the sign, reverse the absolute value, and then apply the sign back. |
Integer.MAX_VALUE (2147483647) | Reversing it will likely cause an overflow and should return 0. |
Integer.MIN_VALUE (-2147483648) | Reversing it will likely cause an overflow and should return 0. |
Reversed integer is slightly larger than Integer.MAX_VALUE (e.g., 2147483649) | Check for overflow before returning the reversed integer; if overflow occurs, return 0. |
Reversed integer is slightly smaller than Integer.MIN_VALUE (e.g., -2147483649) | Check for underflow before returning the reversed integer; if underflow occurs, return 0. |
Single-digit integer like 5 | Reversing it returns the same number, which is a valid case. |
Input is zero (0) | Reversing zero results in zero, which is a valid case and should be handled correctly. |