7. Reverse Integer
mediumAsked at Goldman SachsGoldman Sachs loves Reverse Integer because it forces you to reason about 32-bit overflow without using a big-integer library. Reverse the digits of a signed integer and return 0 if the reversed value overflows the signed 32-bit range.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Goldman Sachs loops.
- Glassdoor (2026-Q1)— Goldman Sachs SWE phone-screen reports list Reverse Integer as a common 'brainteaser-with-overflow-check' question.
- LeetCode Discuss (2025-12)— Reverse Integer appears in the top-10 of LeetCode's Goldman Sachs company tag.
Problem
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.
Constraints
-2^31 <= x <= 2^31 - 1
Examples
Example 1
x = 123321Example 2
x = -123-321Example 3
x = 12021Example 4
x = 15342364690Explanation: Reversing gives 9646324351 which exceeds 2^31 - 1, so we return 0.
Approaches
1. Convert to string and reverse
Convert to string, reverse, parse back, then check the 32-bit bounds.
- Time
- O(log|x|)
- Space
- O(log|x|)
function reverseStr(x) {
const sign = x < 0 ? -1 : 1;
const reversed = sign * parseInt(Math.abs(x).toString().split('').reverse().join(''), 10);
if (reversed < -(2 ** 31) || reversed > 2 ** 31 - 1) return 0;
return reversed;
}Tradeoff: Easy to write but sidesteps the 'no 64-bit integer' constraint — JS Numbers are 64-bit floats so it works in JS but won't translate to Java/C++. Mention it then move to the math version.
2. Digit-by-digit math with overflow check (optimal)
Pop the last digit via modulo, push it onto the result via multiplication. Check overflow BEFORE the push.
- Time
- O(log|x|)
- Space
- O(1)
function reverse(x) {
const INT_MAX = 2 ** 31 - 1;
const INT_MIN = -(2 ** 31);
let result = 0;
while (x !== 0) {
const digit = x % 10;
x = (x - digit) / 10;
if (result > INT_MAX / 10 || (result === Math.trunc(INT_MAX / 10) && digit > 7)) return 0;
if (result < INT_MIN / 10 || (result === Math.trunc(INT_MIN / 10) && digit < -8)) return 0;
result = result * 10 + digit;
}
return result;
}Tradeoff: Constant space and honors the no-64-bit constraint. The overflow check before multiplying is the part Goldman is grading — candidates who check after the fact silently overflow in Java/C++.
Goldman Sachs-specific tips
Goldman Sachs uses this as a sieve for candidates who 'understand numeric edge cases'. The interviewer will ask 'what if you can't use a 64-bit integer?' even if your first answer works. Bring up INT_MAX / 10 vs INT_MAX % 10 = 7 explicitly — that articulation is the moment they decide whether to advance you to round 2. Note: in trading/strats roles overflow is not academic — it's a real production hazard.
Common mistakes
- Checking overflow AFTER the multiplication, which is too late in a 32-bit language.
- Using a sign-stripping trick on -2^31 which itself overflows when negated (since |INT_MIN| > INT_MAX).
- Trailing zeros: 120 reverses to 021 → must drop leading zeros to return 21.
Follow-up questions
An interviewer at Goldman Sachs may pivot to one of these next:
- What if the input is read as a string of arbitrary length? (Reverse in place, watch for the leading zero.)
- What if you needed to support 64-bit input with 64-bit output? (Same math, different bounds.)
- How would you handle palindromic numbers as a follow-up? (Compare original to reversed with overflow guard.)
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does Goldman ask this when JavaScript has 64-bit floats anyway?
Because the language-agnostic version of the problem (Java/C++) is where the overflow story actually matters, and Goldman's loops include Java/C++ rounds. The interviewer wants to see that you'd handle it correctly in those languages — JS-only solutions that ignore the constraint are marked down.
Is there a one-liner that works?
Yes for JS — convert-reverse-parse-check is 4 chained ops. But Goldman is grading your understanding of overflow, not your one-liner skills. Lead with the math version even if you're writing in JS.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Reverse Integer and other Goldman Sachs interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →