123
x (remaining)
Reverse the digits of a 32-bit signed integer — but return 0 if the result would overflow. The trick is to check for overflow before you multiply, so you never need a 64-bit intermediate.
Given a 32-bit signed integer x, return its digits reversed. If the reversed value falls outside the 32-bit signed range [-2 147 483 648, 2 147 483 647], return 0. Example: x = 123 → 321. Example: x = -120 → -21 (trailing zeros vanish). Example: x = 1534236469 → 0 because reversing overflows.
x % 10 and push it onto a running result via result * 10 + digit. The only wrinkle is overflow — and the fix is to check before the multiply: result > (INT_MAX - digit) / 10 tells you if the next push would overflow, without ever computing the overflowed value.result = 0. Fix the 32-bit bounds as constants.digit = x % 10; then x = Math.trunc(x / 10). In JavaScript, % preserves sign, so -123 % 10 === -3 — which is exactly what we want for negative numbers.result > (INT_MAX - digit) / 10, the next push would exceed INT_MAX → return 0. Mirror the check for INT_MIN.result = result * 10 + digit. We only reach here if the overflow check passed.x === 0.if ((result * 10 + digit) > INT_MAX) return 0. In JavaScript this seems to work (JS numbers are 64-bit floats), but it silently fails for very large inputs where the float loses integer precision. Always check before the multiply using the rearranged inequality result > (INT_MAX - digit) / 10.Both approaches are O(d) where d is the number of digits (at most 10 for a 32-bit int). The math approach wins on space and overflow correctness.
123. We will pop digits from the right and push them into the result, checking 32-bit overflow before each multiply.1▶function reverse(x: number): number {2▶ const INT_MAX = 2_147_483_647; // 2^31 - 13▶ const INT_MIN = -2_147_483_648; // -2^3145▶ let result = 0;67 while (x !== 0) {8 const digit = x % 10; // pop last digit (negative-safe in JS)9 x = Math.trunc(x / 10); // drop last digit1011 // Check overflow BEFORE multiplying — avoids 64-bit integer issues12 if (result > Math.trunc((INT_MAX - digit) / 10)) return 0;13 if (result < Math.trunc((INT_MIN - digit) / 10)) return 0;1415 result = result * 10 + digit; // push digit16 }1718 return result;19}
function reverse(x: number): number {
const INT_MAX = 2_147_483_647; // 2^31 - 1
const INT_MIN = -2_147_483_648; // -2^31
let result = 0;
while (x !== 0) {
const digit = x % 10; // pop last digit (negative-safe in JS)
x = Math.trunc(x / 10); // drop last digit
// Check overflow BEFORE multiplying — avoids 64-bit integer issues
if (result > Math.trunc((INT_MAX - digit) / 10)) return 0;
if (result < Math.trunc((INT_MIN - digit) / 10)) return 0;
result = result * 10 + digit; // push digit
}
return result;
}INT_MAX = 2_147_483_647 and INT_MIN = -2_147_483_648. These are the 32-bit signed bounds the problem uses regardless of the host language's native integer width.x % 10 extracts the last digit; sign follows x in JavaScript. Math.trunc(x / 10) drops it (not Math.floor, which would misbehave on negatives).result * 10 + digit > INT_MAX into result > (INT_MAX - digit) / 10 to avoid computing the potentially overflowed value. The same rearrangement handles the negative side with INT_MIN.result = result * 10 + digit shifts the existing digits left by one decimal place and appends the new one. We only reach this line if overflow is impossible.x reaches 0we've processed every digit. result holds the fully reversed integer.Number.MAX_SAFE_INTEGER or use BigInt; the pop/push pattern is unchanged.isNaNand sign handling; it's messier.x = 0 → 0 (loop never runs). x = -120 → -21 (trailing zero vanishes). x = 1_534_236_469 → 0 (overflow).| reverse digits of an integer | x % 10 pop, result * 10 + digit push |
| 32-bit overflow without BigInt | pre-check: result > (INT_MAX - digit) / 10 |
| palindrome number / half-reverse | same pop/push, stop at midpoint |
| string-to-integer with overflow (atoi) | same pre-multiply overflow check |
function reverse(x: number): number {
const INT_MAX = 2_147_483_647;
const INT_MIN = -2_147_483_648;
let result = 0;
while (x !== 0) {
const digit = x % 10;
x = Math.trunc(x / 10);
if (result > Math.trunc((INT_MAX - digit) / 10)) return 0;
if (result < Math.trunc((INT_MIN - digit) / 10)) return 0;
result = result * 10 + digit;
}
return result;
}digit = x % 10; then drop it with x = Math.trunc(x / 10).x = 123. After all iterations, what is result?-120 % 10 equal in JavaScript?x = 1_534_236_469, what should the function return?