Reverse Integer - 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³¹, 2³¹ - 1], then return 0.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

Input & Output

Example 1 — Basic Positive Number
$ Input: x = 123
Output: 321
💡 Note: Reverse the digits: 123 becomes 321. The result fits in 32-bit range so return 321.
Example 2 — Negative Number
$ Input: x = -123
Output: -321
💡 Note: Reverse the digits while preserving the negative sign: -123 becomes -321.
Example 3 — Overflow Case
$ Input: x = 1534236469
Output: 0
💡 Note: Reversing gives 9646324351, which exceeds 2³¹-1 = 2147483647, so return 0.

Constraints

  • -2³¹ ≤ x ≤ 2³¹ - 1

Visualization

Tap to expand
Reverse Integer Problem OverviewInput123Reverse3→2→1Output321Overflow Example:1534236469Input9646324351Exceeds 2³¹-10Return 0🎯 Key: Check overflow before building result
Understanding the Visualization
1
Input
32-bit signed integer x
2
Process
Reverse digits while checking overflow
3
Output
Reversed integer or 0 if overflow
Key Takeaway
🎯 Key Insight: Check for overflow before multiplying by 10 to avoid using 64-bit integers
Asked in
Apple 15 Amazon 12 Bloomberg 8 Microsoft 6
329.3K Views
High Frequency
~15 min Avg. Time
8.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen