Reverse the digits of a signed 32-bit integer. If the reversed value falls outside the 32-bit signed range, return 0. You may not use a 64-bit type to help.
123 → 321-123 → -321120 → 21 (the trailing zero disappears)1534236469 → 0 (reversed, it overflows)
The range is -2^{31} to 2^{31}-1, that is -2{,}147{,}483{,}648 to 2{,}147{,}483{,}647.
Restating the Problem: You are given a 32-bit integer. You need to read its digits backwards to form a new integer. If reversing the digits causes the new number to exceed the 32-bit bounds, you must return 0. You are forbidden from using a 64-bit integer to do the math and check later.
Constraints to note: The strict ban on 64-bit integers means we must detect overflow before it happens.
Edge cases: Inputs ending in zero (e.g. 120 -> 21). Negative inputs (e.g. -123 -> -321).
The naive approach is to convert the integer to a string, reverse the string, parse it back to an integer, and then check if it's outside the [-2^{31}, 2^{31}-1] range. In Python, this works perfectly and never crashes because Python integers don't overflow. However, it completely bypasses the core constraint of the problem (simulating an environment where a 32-bit integer actually overflows and crashes).
while x: x, digit = divmod(x, 10) result = result * 10 + digit
Trailing zeros vanish for free, because a leading 0 in the result contributes nothing.
The entire difficulty is the overflow check, and the constraint is what makes it interesting: you cannot compute the value and then check, because in a fixed-width language the computation has already wrapped by then.
INT_MAX // 10 is 214,748,364, and the last digit of INT_MAX is 7.
Read it in words: if the accumulated value is already bigger than a tenth of the limit, multiplying by 10 will exceed it. If it is exactly a tenth of the limit, only a final digit above 7 pushes it over.
The negative side mirrors it, with INT_MIN // 10 = -214748364 and a last digit of 8. That is because INT_MIN is one further from zero than INT_MAX, a lopsidedness built into how computers store negative numbers.
In practice the boundary-digit cases cannot occur, because the input itself must have been a valid 32-bit integer, so its reversal cannot land exactly on the boundary with an extra digit. Write the check anyway — the interviewer is testing whether you thought about it, and reasoning "it cannot happen" without the check is weaker than including it.
(1) The 32-bit limits. The problem says to return 0 if the reversed number does not fit between them.
(2) Remember the sign, and
(3) work with the positive value. Reversing digits is simpler without a minus sign in the way.
(4) While digits are left,
(5) peel off the last digit.divmod(x, 10) returns x without its last digit, and that last digit.
(6) Check for overflow before it happens. The next line computes result * 10 + digit. That is too big exactly when result is already bigger than INT_MAX // 10, or equal to it with a last digit above 7. (INT_MAX is 2,147,483,647, so INT_MAX // 10 is 214,748,364 and its last digit is 7.) The problem imagines a machine that cannot hold a bigger number even for a moment, so the check must come before the multiplication, not after.
(7) Add the digit to the end of the reversed number. Multiplying by 10 shifts every digit one place left, making room.
(8) Put the sign back.
(9) Return it if it fits, otherwise 0.
Code Walkthrough:
Initialization: We extract the sign and work with the absolute value x = abs(x). We initialize result = 0.
Digit Extraction Loop: While x > 0, we pop the last digit using modulo (divmod(x, 10)).
Pre-emptive Overflow Check: Before we multiply result by 10 and add the digit, we check if result is already greater than INT_MAX // 10. If it is, the next multiplication will guarantee an overflow. If it's exactly equal to INT_MAX // 10, we check if the new digit is greater than 7 (since 2^{31}-1 ends in 7). If either is true, we return 0.
Accumulation: If safe, we execute result = result * 10 + digit.
Finalization: We reapply the sign. We add a final boundary check as a safety net (though technically our loop logic handles it).
Taking the sign out first removes every question about how negative division and modulo behave, which differ between languages. Python's divmod(-13, 10) gives (-2, 7), not (-1, -3) — the floor-versus-truncate issue from 4.8.3. Working with the absolute value sidesteps it entirely.
abs(INT_MIN) would itself overflow in a fixed-width language, since 2^{31} does not fit. In Python it is fine. In Java you would handle the sign differently, or widen to long. Worth naming.
The final range check is belt and braces in Python, where the loop's check has already done the work, but it makes the intent explicit.
O(\log x) time — the number of digits. O(1) space.
Time Complexity:O(\log_{10} x). The number of digits in x determines how many times the loop runs, which is roughly \log_{10}(x). Since x is bounded by 32 bits, this is at most 10 iterations, making it O(1) in practice.
Space Complexity:O(1). We only use a few integer variables, avoiding strings or arrays entirely.
Python integers are unbounded, so nothing overflows and the check is pure simulation of a constraint the language does not have. That can make the problem feel artificial.
It is not. In Java, C, C++, Go and Rust the overflow is real, silent, and the source of genuine bugs — a wrapped value looks like a plausible number and propagates. Signed integer overflow is undefined behaviour in C and C++, which means the compiler may assume it never happens and optimise accordingly, producing behaviour that surprises everyone.
Say this if asked why the check matters. It reframes an artificial-feeling exercise as a defensive habit.
String to Integer (atoi) (LeetCode 8) — the same overflow check plus whitespace, sign and non-digit handling. A notoriously fiddly problem for exactly that reason.
Divide Two Integers (LeetCode 29) — the famous case is INT_MIN / -1, whose true answer is one past INT_MAX.
Palindrome Number — reverse half the number instead of all of it, which avoids overflow entirely. A neat demonstration that the best fix for overflow is often to not create the large value at all.
Chapter 1.3 covers two's complement and why INT_MIN has no positive counterpart.
"How do you detect overflow without a wider type?" Check result against INT_MAX // 10 before multiplying. Derive it rather than reciting it.
"Why 7 and 8?" The last digits of INT_MAX and INT_MIN.
"Why is INT_MIN different?" Two's complement has one more negative value than positive, so abs(INT_MIN) does not fit.
"How do you handle the sign?" Strip it first, so negative division behaviour never arises.
"Does this matter in Python?" No — and say why it matters everywhere else, including undefined behaviour in C.
One thing to volunteer: derive the boundary check out loud instead of quoting it. That derivation is the whole problem, and it is the difference between a memorised solution and a considered one.
Next:4.31 is the last chapter of Part 4 — KMP, Z, rolling hashes and Manacher, the string algorithms that sit one level above everything the NeetCode 150 asks for.