Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Base case `n == 0` returns 0; else return `n % 10 + sumDigits(n / 10)`.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you apply the digit idiom recursively with the right base case.
Where candidates slip
Wrong base case, or mishandling trailing zeros/negatives.
How to stand out
Mention the digital-root follow-up (repeat until one digit).
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
Peel last digit
1Hint 1Explore hintHide hint
What's the last digit, and what's 'the rest'?
2Hint 2Explore hintHide hint
sum(n) = n%10 + sum(n/10).
3Hint 3Explore hintHide hint
Which n is the base case — 0 or 1?
A · Core idea
sum(n) = n%10 + sum(n/10), base case sum(0) = 0 — the digit-peeling idiom expressed recursively.
Why %10 and /10. n % 10 is the last digit (added now); n / 10 removes it (the smaller subproblem). Each call shortens the number by one digit until nothing is left, so the recursion depth equals the digit count.
Why base case n == 0, not n == 1. When n reaches 0 there are no digits left to add, so it contributes 0. Stopping at 1 would mishandle numbers ending in 0 and the input 0 itself.
Negatives. If negatives are allowed, take the absolute value first — the sign isn't a digit. Depth is small (≤ ~10 for ints), so stack isn't a concern here.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 12341234
4 + sum(123)
waits10C · Code with teaching comments
D · Why it works and complexityO(d) digits timeO(d) stack spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(d) digits time, O(d) stack space — one call per digit (≈ log10 n), each O(1).
E · Edge cases and pitfalls5n = 0 — Base case → 0.Explore edge casesHide edge cases
Edge cases handled
n = 0Base case → 0.n = 99 + sum(0) = 9.n = 1001 + 0 + 0 = 1.
Pitfalls
- Negative inputs not handled (take abs first if allowed).
- Base case at n == 1 instead of n == 0.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int sumDigits(int n) { if (n == 0) return 0; return n % 10 + sumDigits(n / 10); }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Negative inputs not handled (take abs first if allowed).
- Base case at n == 1 instead of n == 0.
Revise
Carry the pattern into the interview
Pattern to remember
Peel last digit
Code pattern to memorize
sum(n) = n==0 ? 0 : n%10 + sum(n/10)Rules to remember
- 1digit = n%10.
- 2rest = n/10.
- 3Base n==0 → 0.
Use this pattern when
- Digit aggregation recursively
Interview takeaway
%10 and /10 peel digits one at a time.
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: Peel last digit. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Sum of DigitsThe iterative loop version.
- Add Digits (digital root)Coming soonRepeat the digit-sum to one digit.