7. Reverse Integer
mediumAsked at IntelGiven a signed 32-bit integer x, return x with its digits reversed. If the reversed number overflows 32-bit range, return 0. Intel asks because the entire problem is an overflow-detection puzzle in disguise — the candidate who pre-checks for overflow before multiplying is the candidate who would catch the same bug in firmware.
By Sam K., Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Intel loops.
- Glassdoor (2026-Q1)— Intel SWE phone-screen reports list reverse-integer as a recurring overflow-edge-case round.
- GeeksforGeeks (2025-09)— Intel Interview Experience archives reference reverse-integer with explicit INT_MAX/INT_MIN discussion.
- LeetCode discuss (2025-10)— Intel-tagged in the company-frequency listing.
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
x = 123321Example 2
x = -123-321Example 3
x = 12021Example 4
x = 15342364690Explanation: Reversed = 9646324351 which exceeds INT_MAX (2147483647), so return 0.
Approaches
1. String reverse (brute)
Convert to string, strip sign, reverse, parse back, check range.
- Time
- O(log x) = number of digits
- Space
- O(log x) for the string
function reverseStr(x) {
const sign = x < 0 ? -1 : 1;
const s = Math.abs(x).toString().split('').reverse().join('');
const result = sign * parseInt(s, 10);
if (result < -(2 ** 31) || result > 2 ** 31 - 1) return 0;
return result;
}Tradeoff: Works in JS but violates the problem's no-64-bit-storage constraint conceptually. Also allocates a string. The interviewer wants the integer-arithmetic version.
2. Digit-by-digit with pre-overflow check (optimal)
Pop the last digit of x with %10, append to result via result = result*10 + digit. Before each multiplication, check that result will not exceed INT_MAX/10 or fall below INT_MIN/10.
- Time
- O(log x)
- Space
- O(1)
function reverse(x) {
const INT_MAX = 2 ** 31 - 1; // 2147483647
const INT_MIN = -(2 ** 31); // -2147483648
let result = 0;
while (x !== 0) {
const digit = x % 10;
x = (x - digit) / 10; // truncation toward zero, equivalent to C-style int div
// pre-overflow check
if (result > Math.floor(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: Constant space, no string allocation, overflow caught BEFORE the multiplication that would cause it. The pre-check pattern is what Intel wants to see — it's the same pattern you'd write in C to defend against UB.
Intel-specific tips
Intel rounds for hardware-adjacent SWE will explicitly ask 'what if you can't use 64-bit ints?'. The answer is the pre-overflow check: compare against INT_MAX/10 BEFORE multiplying. Walking through 'INT_MAX = 2147483647, so any result > 214748364 will overflow on the next *10, and exactly 214748364 overflows iff the next digit is > 7' shows you understand the boundary arithmetic — that's the senior signal here.
Common mistakes
- Multiplying first then checking — by then it's too late, the overflow already happened (and in C is undefined behavior).
- Using string conversion and missing the 'cannot store 64-bit' constraint — works on the LC judge but loses the point.
- Sign handling: in JavaScript, -7 % 10 === -7 (truncation toward zero), but in Python it's 3 (floor division). Hand-trace the sign for negative inputs.
- Forgetting that `INT_MAX` ends in 7 and `INT_MIN` ends in 8 — the asymmetric digit check (>7 for positive, <-8 for negative) is easy to flip.
Follow-up questions
An interviewer at Intel may pivot to one of these next:
- String to Integer (atoi) — LC 8. Same overflow-checking discipline.
- Palindrome Number — LC 9. Reverse half the number to compare without overflow risk.
- What if you had to reverse a fixed-point number in 32-bit hardware? (Same loop; track the implied decimal point separately.)
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is the pre-overflow check 'INT_MAX / 10 then digit > 7'?
INT_MAX = 2147483647. INT_MAX / 10 = 214748364 (integer division). Any result strictly larger than 214748364 will exceed INT_MAX when multiplied by 10. If result equals 214748364 exactly, then the next digit must be <= 7 to stay within INT_MAX (since INT_MAX's last digit is 7).
Does JavaScript's number type matter here?
Conceptually no — the algorithm is pure integer math. JS numbers are 64-bit floats with 53 bits of integer precision, so 2^31 * 10 fits fine. We mimic 32-bit-int behavior to match the problem's stated constraint.
Free learning resources
Curated free links for this problem.
More Intel coding interview questions
- 8. String to Integer (atoi)
- 9. Palindrome Number
- 26. Remove Duplicates from Sorted Array
- 50. Pow(x, n)
- 53. Maximum Subarray
- 66. Plus One
- 69. Sqrt(x)
- 70. Climbing Stairs