Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Work with `abs(n)`. If it's 0 return 1. Otherwise divide by 10 until it hits 0, counting each division.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you know the divide-by-10 digit idiom and handle the 0 / negative boundaries.
Where candidates slip
Off-by-one on zero, or a loop that never runs for negatives.
How to stand out
Mention the log10 O(1) trick and why the loop is safer (no float rounding).
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
Divide by 10
1Hint 1Explore hintHide hint
How many times can you divide the number by 10 before it becomes 0?
2Hint 2Explore hintHide hint
Use `% 10`/`/ 10` — here you only need the `/ 10` to strip digits and count.
3Hint 3Explore hintHide hint
Handle 0 (one digit) and negatives (take abs) explicitly.
A · Core idea
The digit count of a number is how many times you can integer-divide it by 10 before it reaches 0. Each /= 10 chops the last digit; count the chops.
Why `% 10` and `/ 10` are the digit toolkit. n % 10 reads the last digit; n / 10 removes it. Every digit problem — count, sum, reverse, palindrome — is built from this pair. Here we only need the removal half, counting iterations.
Why zero needs a special return. The loop condition n > 0 is false immediately for n = 0, so it would count 0 digits — but '0' has one digit. Return 1 up front for that input.
Why take the absolute value. A negative like -42 has 2 digits; the sign isn't a digit. Working with abs(n) makes the loop behave for negatives (and avoids n > 0 terminating instantly on a negative).
The `log10` alternative. (int) Math.log10(abs(n)) + 1 is O(1), but it's vulnerable to floating-point rounding on exact powers of ten — the divide loop is the robust answer.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 1234512345 / 10
= 1234
count = 15C · Code with teaching comments
D · Why it works and complexityO(d) where d = number of digits timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
A slick alternative is (int) Math.log10(abs(n)) + 1, but the divide loop is safer and avoids floating-point edge cases.
ComplexityO(d) where d = number of digits time, O(1) space — one division per digit, so it's proportional to the number of digits d ≈ log10(n).
E · Edge cases and pitfalls5n = 0 — Loop never runs; the up-front `return 1` gives the right answer.Explore edge casesHide edge cases
Edge cases handled
n = 0Loop never runs; the up-front `return 1` gives the right answer.n = -42abs makes it 42 → 2 digits; sign ignored.n = 9Single division to 0 → 1 digit.
Pitfalls
- Returning 0 for input 0 instead of 1.
- Forgetting `abs`, so the `n > 0` loop ends immediately on a negative.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int countDigits(int n) { n = Math.abs(n); if (n == 0) return 1; int count = 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.
- Returning 0 for input 0.
- Forgetting to take the absolute value, so the loop misbehaves on negatives.
Revise
Carry the pattern into the interview
Pattern to remember
Strip digits with /10
Code pattern to memorize
count = 0; while n>0: n//=10; count++ (handle 0 -> 1)Rules to remember
- 1Use abs(n).
- 2Special-case 0 → 1.
- 3Each /10 removes one digit.
Use this pattern when
- Anything that processes a number digit by digit
Interview takeaway
The divide-by-10 loop is the backbone of every digit problem.
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: Strip digits with /10. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
Same pattern