Skip to main content

7. Reverse Integer

mediumAsked at Apple

Reverse Integer is Apple's deceptively-simple overflow-detection question. The reversal loop is three lines; the real challenge is detecting 32-bit overflow BEFORE it happens. Apple grades on whether you remember -2^31 to 2^31 - 1 and check the bound on every digit append.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Apple loops.

  • Glassdoor (2026-Q1)Apple SWE phone-screen reports list Reverse Integer as a recurring easy/medium with the overflow follow-up.
  • Blind (2025-11)Apple new-grad and ICT3 reports cite Reverse Integer as the canonical overflow-handling question.

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

Approaches

1. Digit-by-digit reversal with pre-overflow check (optimal)

Repeatedly pop the last digit of x and push it onto result. Before each push, check whether result * 10 + digit would overflow.

Time
O(log10 x)
Space
O(1)
function reverse(x) {
  const INT_MAX = 2147483647;
  const INT_MIN = -2147483648;
  let result = 0;
  while (x !== 0) {
    const digit = x % 10;
    x = (x - digit) / 10;
    if (result > INT_MAX / 10 || (result === Math.floor(INT_MAX / 10) && digit > 7)) return 0;
    if (result < Math.ceil(INT_MIN / 10) || (result === Math.ceil(INT_MIN / 10) && digit < -8)) return 0;
    result = result * 10 + digit;
  }
  return result;
}

Tradeoff: Logarithmic in x (up to ~10 digits). The PRE-multiply overflow check is the whole interview — most candidates either skip it entirely or do post-check which is already corrupted in true 32-bit math. JS uses doubles so the multiply wouldn't truly overflow, but the constraint says assume no 64-bit storage.

2. String conversion (lazy)

Convert to string, reverse, parse back. Handle sign and overflow separately.

Time
O(log x)
Space
O(log x)
function reverse(x) {
  const sign = x < 0 ? -1 : 1;
  const str = Math.abs(x).toString();
  const reversed = str.split('').reverse().join('');
  const result = sign * parseInt(reversed, 10);
  if (result < -2147483648 || result > 2147483647) return 0;
  return result;
}

Tradeoff: Works in JavaScript because doubles handle the intermediate value. Apple will reject this and ask for the constraint-compliant version — the problem explicitly forbids 64-bit storage to force the pre-check.

Apple-specific tips

Apple is testing whether you remember the 32-bit bounds and check BEFORE the multiply, not after. The check 'result > INT_MAX / 10' isn't quite right by itself — INT_MAX = 2147483647 so INT_MAX/10 = 214748364, and any positive digit on top of that would overflow. The 'or result == INT_MAX/10 and digit > 7' clause handles the exact-boundary case. Walk through 1534236469 (which reverses to overflow) to convince the interviewer your bound is tight.

Common mistakes

  • Checking overflow AFTER the multiply — already too late in true 32-bit.
  • Forgetting the sign — in JavaScript -123 % 10 returns -3, which actually works correctly here; in some languages this is the bug surface.
  • Returning Math.floor or unary minus on the wrong half of the boundary check.

Follow-up questions

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

  • String to Integer (atoi) (LC 8) — similar overflow handling.
  • Palindrome Number (LC 9) — reverse digits, compare to original.
  • What if we needed to reverse arbitrary-precision BigInts?

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 boundary check so fiddly?

Because INT_MAX = 2147483647 ends in 7, not a round number. A pre-multiply check must allow result == INT_MAX/10 ONLY when the next digit is <= 7, otherwise reject.

Does JavaScript's number type actually overflow?

Not at 2^31 — JS uses 53-bit doubles. The problem still requires us to enforce the 32-bit bound as a contract. Use the explicit comparison.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

Drill Reverse Integer and other Apple interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →