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
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To solve this problem, we'll reverse the digits of the integer x, while ensuring that:
We do not use 64-bit integers.
We stay inside the 32-bit signed integer range, which is:
[
[-2^{31}, 2^{31}-1] = [-2147483648, 2147483647]
]
✅ Strategy:
We will:
Extract digits using integer division and modulus.
Build the reversed number step-by-step.
Check for overflow before each multiplication and addition.
Let’s write this in Python, which naturally handles big integers, but we'll simulate the 32-bit constraint.
💻 Python Code:
python
def reverse(x: int) -> int:
INT_MAX = 2**31 - 1 # 2147483647
INT_MIN = -2**31 # -2147483648
result = 0
negative = x < 0
x = abs(x)
while x != 0:
digit = x % 10
x = x // 10
# Check for overflow before appending digit
if result > (INT_MAX - digit) // 10:
return 0 # Will overflow on next step
result = result * 10 + digit
return -result if negative else result
🔍 Explanation:
We extract each digit using x % 10 and reduce x by x //= 10.
Before enlarging result with result * 10 + digit, we check:
python
result > (INT_MAX - digit) // 10
If this is True, adding the new digit would overflow 32-bit.
Let me know if you'd like it in another language like Java, C++, or JavaScript!
User
do you need to check for overflow before adding the digit?
Assistant
That's a great question! ✅
Yes, you absolutely need to check for overflow before adding the digit to your result when reversing the number, especially because you're constrained to 32-bit signed integers, and cannot use 64-bit integers to handle larger calculations.
🔥 Why check before adding the digit?
Let's break it down:
Suppose you're processing the number in this reverse-building loop:
python
result = result * 10 + digit
Now imagine result = 214748364, and you're trying to append 7 to it. That will become:
code
result = 214748364 * 10 + 7 = 2147483647
✅ That’s the max 32-bit signed int, so it's still valid.
But if you try to append 8, you’d get:
code
result = 214748364 * 10 + 8 = 2147483648
❌ BOOM! That’s overflow!2147483648 is out of the 32-bit signed integer range.
Once you're in overflow territory, the reversed result is undefined (or should return 0) according to the problem statement.
💡 Why not check after adding the digit?
If you perform the multiplication and addition first, and then check for overflow, it's too late — the operation itself could already overflow internally (inside the CPU or language implementation) before you even do the check!
In some languages like C/C++ or Java, this could cause undefined behavior — or silently give the wrong result if an overflow occurs during arithmetic.
That's why we must check beforehand whether doing result * 10 + digit would cause an overflow.
✅ The Safe Check in Code:
python
if result > (INT_MAX - digit) // 10:
return 0
This says:
Before adding digit, check if result * 10 + digit would exceed INT_MAX.
This works because:
[
\text{result * 10 + digit} > \text{INT_MAX} \Rightarrow \text{result} > \frac{\text{INT_MAX - digit}}{10}
]
So, by proactively checking, we avoid triggering actual overflow.
🧠 TL;DR
Yes, you must check for overflow before multiplying and adding digits to the reversed number to avoid: