← Whiteboard questions

Reverse Integer

Reverse the digits of a 32-bit signed integer; return 0 on overflow

DSA, Medium, Bit Manipulation

Which structure or algorithm, and what does it cost.

Given a whole number x, return the number you get when its decimal digits are written in reverse order. Keep the sign and drop any leading zeros the reversal makes. The result must fit a 32-bit signed value, from -2,147,483,648 to 2,147,483,647. If it does not fit, return 0. In a language with no fixed integer width, such as Python, your code must make that check itself. In a language with 32-bit integers, make the check before the value can wrap. No bit operations are needed; the 32-bit limit is the point.

Examples

Input x = 7160
Output 617
Why the reversed digits are 0617, and the leading zero is dropped.
Input x = -4820
Output -284
Why the sign stays. The digits 4820 reverse to 0284, which is 284.
Input x = 1999999999
Output 0
Why reversed it is 9999999991, which is above 2,147,483,647, so the answer is 0.

Constraints

Your answer must contain

Target Aim for O(d) time for d digits, at most 10, and O(1) memory.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

Look at the number from its right end. Ask how to take one digit off, and how to put it at the start of a new number.

Hint 2 the approach

Pop a digit: the remainder after dividing by 10 is the last digit, and dividing by 10 leaves the rest. Take digits this way until nothing is left.

Hint 3 the key step

Make room for one more digit, then add it. Decide first what to do with the sign, and where the 32-bit check goes in your language.

Walkthrough

Full walkthrough brute force to best

Brute force

Turn the absolute value into text, reverse the text, turn it back into a number, put the sign back, and check the range. This is correct and short in Python. It costs O(d) time for d digits, at most 10, and O(d) memory for the text.

Best method

Pop the last digit with the remainder after dividing by 10. Push it by multiplying the new number by 10 and adding the digit. Each pop takes a digit from the end, and each push places it after the earlier ones, so the digits come out in reverse order. A leading zero of the result vanishes on its own, because 0 times 10 plus the next digit is just that digit. Work on the absolute value, since the remainder of a negative number differs between languages, and put the sign back at the end. Python never overflows, so compare the final value with the 32-bit range and return 0 if it is outside. A language with 32-bit integers must check before each push.

Hand trace

Run on 7160. Take the absolute value, 7160, and start the result at 0. Pop 0: the result is 0 and 716 is left. Pop 6: the result is 6 and 71 is left. Pop 1: the result is 61 and 7 is left. Pop 7: the result is 617 and 0 is left. The sign is positive and 617 fits in 32 bits, so return 617.

Code

def reverse_integer(x):
    sign = -1 if x < 0 else 1
    x = abs(x)
    result = 0
    while x:
        result = result * 10 + x % 10     # push the last digit
        x //= 10                          # pop it
    result *= sign
    return result if -2**31 <= result <= 2**31 - 1 else 0

Time and memory

O(d) time, with one round per digit and at most 10 rounds. O(1) memory.

Common mistakes

  • Skipping the range check. For 1999999999 the reversed number is 9999999991, which does not fit and must give 0.
  • Taking the remainder of a negative number directly. In Python, -123 % 10 is 7, and -1 // 10 is -1, so the loop never ends. Work on the absolute value.
  • Dropping the sign. The number -4820 must give -284, not 284.

Related problems

More Bit Manipulation problems
Reverse Integer: hints, walkthrough and Python solution