#7 Reverse Integer
frequently reported atBloombergAppleGoogleMicrosoftAmazonTCS
the problem
In plain words
You get a signed 32-bit int x. Return x with its digits in reverse order, keeping the sign. If the reversed value does not fit in a signed 32-bit int (−2³¹ to 2³¹ − 1), return 0. Pretend you cannot store 64-bit values.
in x = 123
out 321
in x = -123
out -321
in x = 120
out 21
The trailing zero becomes a leading zero and disappears.
- −2³¹ ≤ x ≤ 2³¹ − 1
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternDigit peeling (% 10 and / 10)
where this runsThe JDK's own Integer.toString peels digits the same way — two per step — and works on the negative value so that Integer.MIN_VALUE needs no special case.
Pop digits with % 10 and push them with rev * 10 + pop. The only hard part is overflow: compare rev with Integer.MAX_VALUE / 10 before the multiply, because after it the value has already wrapped.
solutions
From brute force to optimal
Reverse the digits as a string, parse into a long, range-check.
- digits = reverse of Long.toString(|x|).
- rev = Long.parseLong(digits), negate if x < 0.
- Out of int range → 0.
d ≤ 10 digits, but with a String and a StringBuilder.
The temporary strings — and it uses 64-bit storage, which the problem forbids.
class Solution { public int reverse(int x) { String digits = new StringBuilder(Long.toString(Math.abs((long) x))).reverse().toString(); long rev = Long.parseLong(digits); // at most 10 digits: fits in a long if (x < 0) rev = -rev; if (rev < Integer.MIN_VALUE || rev > Integer.MAX_VALUE) return 0; return (int) rev; }}Same digit loop, but in a long that can't overflow on 10 digits; check the range at the end.
- rev = rev * 10 + x % 10; x /= 10 until x is 0.
- Return rev if it fits in an int, else 0.
One step per digit.
One long — correct, but it breaks the "no 64-bit" rule.
class Solution { public int reverse(int x) { long rev = 0; // a long cannot overflow on 10 digits... while (x != 0) { rev = rev * 10 + x % 10; x /= 10; } return rev == (int) rev ? (int) rev : 0; // ...but the problem forbids 64-bit storage }}Pop the last digit, check that rev * 10 + pop still fits, then push.
- pop = x % 10 (negative for negative x); x /= 10.
- If rev > MAX/10, or rev == MAX/10 and pop > 7 → return 0. Mirror for MIN/10 and pop < −8.
- rev = rev * 10 + pop.
At most 10 loop steps for an int.
Two ints.
class Solution { public int reverse(int x) { int rev = 0; while (x != 0) { int pop = x % 10; x /= 10; if (rev > Integer.MAX_VALUE / 10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return 0; if (rev < Integer.MIN_VALUE / 10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0; rev = rev * 10 + pop; } return rev; }}Dry run
in the room
How to say it in the interview
talk track
- Point out that Java's
%keeps the sign, so one loop handles negative x too. - Say where overflow can happen (
rev * 10) and why you check before it, not after. - Mention that the
== MAX/10case can't actually overflow for a 10-digit input (its first digit is at most 2), but the full check is safer and costs nothing.
traps
- Checking
rev * 10 + pop > Integer.MAX_VALUE— that expression has already wrapped. - Taking
Math.abs(x)first:Math.abs(Integer.MIN_VALUE)is still negative.
what they ask next
Follow-ups
String to Integer (atoi), LC 8?
Same guard while appending digits from a string — clamp to MAX_VALUE or MIN_VALUE instead of returning 0.
before you submit
Edge cases to test
- 0 → 0
- Trailing zeros: 120 → 21
- 1534236469 → 0 (overflows on the last digit)
- Integer.MIN_VALUE → 0 (and Math.abs(MIN_VALUE) is still negative)
practise next
Similar problems
- #9Palindrome NumberEasyin Math & Numbers
- #66Plus OneEasyin Math & Numbers