JavaEasyNumbers10 min
Reverse the digits of an integer
Return the reversed integer, preserving its sign and discarding leading zeros. Follow the stated ordering and edge-case conventions exactly. Interviewers use this task to probe algorithm selection, invariants, and boundary handling.
Input
One signed integer n.
Output
Return the reversed integer, preserving its sign and discarding leading zeros.
Constraints
-2147483648 <= n <= 2147483647.
Example
Input
120
Output
21
Example 2 input
1000000000
Output
1
This example exercises trailing zero removal.
Public test cases (2)
Input
120
Expected output
21
Input
-450
Expected output
-54
Hints (2)
- Identify the state needed to avoid recomputing work for each element.
- Accumulate the final digit into a long and remove it from the input.
Target complexity: time O(d) · space O(1)
