Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Track the sign, work with the absolute value, and repeatedly do `rev = rev * 10 + n % 10; n /= 10`. Reapply the sign at the end.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you can construct a number with the ×10+digit idiom and reason about sign and overflow.
Where candidates slip
Dropping the sign, or overflowing on a reverse that exceeds int range.
How to stand out
Add the pre-multiply overflow check (the LC7 detail) even if the prompt says it fits.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Build reversed via *10
1Hint 1Explore hintHide hint
Build the result one digit at a time from the input's last digit.
2Hint 2Explore hintHide hint
`rev = rev * 10 + n % 10` appends a digit; `n /= 10` advances.
3Hint 3Explore hintHide hint
Track the sign separately and think about whether the reversed value can overflow.
A · Core idea
Build the reversed number digit by digit: rev = rev * 10 + n % 10 pulls the current last digit of n and appends it to rev, while n /= 10 advances. Repeat until n is gone.
Why `rev * 10 + digit` builds a number. Multiplying by 10 shifts every existing digit one place left (opening a units slot), and adding the new digit fills that slot. Feeding digits from the right of n appends them to the right of rev — which reverses the order. This 'accumulator × base + digit' is the universal way to construct a number left-to-right.
Why trailing zeros vanish. For 120, the digits fed in are 0, 2, 1. The leading 0 contributes 0*10 + 0 = 0, so it never becomes a stored leading zero — you get 21, which is the conventional answer.
Sign and overflow. Save the sign, reverse the absolute value, reapply the sign. In production, reversing can overflow (reverse of 2,000,000,003 exceeds int), so a robust version checks rev against the limit before each rev*10. Mentioning that guard earns credit even when the prompt promises it fits.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 1234digit 4
rev = 0*10 + 4
rev = 4, n = 1234321C · Code with teaching comments
D · Why it works and complexityO(d) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
In production you'd also guard against overflow before rev * 10 + digit; mentioning that earns credit even if the prompt promises it fits.
ComplexityO(d) time, O(1) space — one multiply/add per digit, with a single accumulator — proportional to the digit count.
E · Edge cases and pitfalls5n = -120 — Sign saved; reverse of 120 is 21; result -21 (trailing zero dropped).Explore edge casesHide edge cases
Edge cases handled
n = -120Sign saved; reverse of 120 is 21; result -21 (trailing zero dropped).n = 5Single digit reverses to itself.n = 1000Reverses to 1; leading zeros never stored.
Pitfalls
- Losing the sign on negative inputs.
- Ignoring overflow when the reversed value can exceed the type's range.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int reverse(int n) { int sign = n < 0 ? -1 : 1; n = Math.abs(n); int rev = 0; while (n > 0) {Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Losing the sign on negative inputs.
- Ignoring overflow when the reversed value can exceed int range.
Revise
Carry the pattern into the interview
Pattern to remember
Digit build-up (`rev*10 + d`)
Code pattern to memorize
rev = rev*10 + n%10; n /= 10Rules to remember
- 1Save sign, use abs.
- 2
rev = rev*10 + n%10. - 3Trailing zeros disappear.
Use this pattern when
- Reversing digits
- Building any number left-to-right from a digit stream
Interview takeaway
`acc * 10 + digit` is how you build any number left-to-right.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Digit build-up (`rev*10 + d`). Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Reverse Integer (with overflow)Coming soonSame idiom but return 0 on 32-bit overflow.
- Palindrome NumberReverse and compare to the original.
Same pattern