Skip to main content

7. Reverse Integer

mediumAsked at Bloomberg

Reverse the digits of a 32-bit signed integer, returning 0 on overflow. Bloomberg uses this specifically to test whether you handle the 32-bit integer overflow check correctly — finance code at Bloomberg routinely cares about numeric boundaries.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Bloomberg loops.

  • Glassdoor (2026-Q1)Recurring in Bloomberg phone screens — interviewer asks specifically for the overflow handling story.
  • Blind (2025-11)Bloomberg new-grad SWE reports note the 32-bit overflow trap as the actual grading criterion.

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 (signed or unsigned).

Constraints

  • -2^31 <= x <= 2^31 - 1

Examples

Example 1

Input
x = 123
Output
321

Example 2

Input
x = -123
Output
-321

Example 3

Input
x = 120
Output
21

Example 4

Input
x = 1534236469
Output
0

Explanation: Reversed it overflows 32-bit signed range.

Approaches

1. Digit-by-digit pop/push with overflow check

Pop the last digit with mod/div, push onto the result with multiply/add. Check overflow BEFORE the push.

Time
O(log10(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 < Math.ceil(INT_MIN / 10) || (result === Math.trunc(INT_MIN / 10) && digit < -8)) return 0;
    result = result * 10 + digit;
  }
  return result;
}

Tradeoff: The textbook answer. Note JavaScript stores all numbers as 64-bit doubles, so we SIMULATE the 32-bit constraint manually — which is exactly what Bloomberg's grading rubric checks.

2. String reverse (anti-pattern)

Convert to string, reverse, parse back. Then range-check.

Time
O(log10(x))
Space
O(log10(x))
function reverseString(x) {
  const INT_MAX = 2 ** 31 - 1;
  const INT_MIN = -(2 ** 31);
  const sign = x < 0 ? -1 : 1;
  const reversed = sign * parseInt(String(Math.abs(x)).split('').reverse().join(''), 10);
  if (reversed > INT_MAX || reversed < INT_MIN) return 0;
  return reversed;
}

Tradeoff: Easy to read but allocates strings + uses the 64-bit-double escape hatch the problem forbids. Bloomberg interviewers specifically want the digit-by-digit version because it shows you can reason about numeric bounds.

Bloomberg-specific tips

Bloomberg cares specifically about the OVERFLOW CHECK. State out loud: 'I need to check before the multiply, not after, because the multiply can already overflow.' That single sentence often differentiates strong from average candidates on this problem.

Common mistakes

  • Checking overflow AFTER result = result * 10 + digit (in a true 32-bit env, the multiply already overflowed).
  • Forgetting that JavaScript numbers are 64-bit doubles — you must simulate the 32-bit boundary explicitly.
  • Mishandling negative numbers — JS modulo of a negative is negative, which actually helps here, but C++ candidates often trip on this.

Follow-up questions

An interviewer at Bloomberg may pivot to one of these next:

  • Palindrome Number (LC 9) — reuse the reverse trick.
  • String to Integer / atoi (LC 8) — parse with overflow guard.
  • Reverse Bits (LC 190) — same shape but binary.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why is the overflow check the actual interview signal?

Because the obvious solution (reverse digits) is one loop; only handling overflow correctly takes care. Bloomberg uses this gap to grade rigor.

Should I use BigInt in JavaScript?

No. The problem explicitly forbids 64-bit storage, so simulating 32-bit with manual checks is what the rubric grades.

Free learning resources

Curated free links for this problem.

Companies that also ask Reverse Integer