LC 7 — 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, return 0.

Input: x = 123
Output: 321
Input: x = -123
Output: -321
Input: x = 120
Output: 21

Constraints: -231 ≤ x ≤ 231 - 1 (i.e. -2,14,74,83,648 to 2,14,74,83,647)

Given an integer like 123, you need to produce 321. You cannot convert to a string — the problem explicitly forbids it. No BigInt, no 64-bit types. The digits are trapped inside the number. How do you extract them one by one, using only arithmetic?

Drag the scrubber below and watch the trace table. Pay attention to how each digit comes out and how the result builds up. The mechanism will reveal itself.

FIG. 1 — LIVE REVERSAL SIMULATOR
0x = 1233,00,00,00,000
#digitx afterresult
13123
22132
310321

Try dragging to bigger numbers. What happens near 1.5 billion?