Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Three counters; for each value compare against 0 and bump the matching bucket.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you enumerate disjoint categories with a clean three-way branch.
Where candidates slip
Mishandling zero or overlapping conditions.
How to stand out
Note this is the sign() branch applied in a counting loop.
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
Three buckets
1Hint 1Explore hintHide hint
How many distinct categories are there? One counter each.
2Hint 2Explore hintHide hint
Make zero its own branch — it's neither positive nor negative.
3Hint 3Explore hintHide hint
Use mutually-exclusive if/else-if so each element is counted once.
A · Core idea
Classify each value as positive, negative, or zero and tally three counters — the three-bucket extension of the sign-of-a-number branch.
Why zero is its own bucket. Zero is neither positive nor negative, so it needs an explicit else branch. Lumping it with positives (>= 0) is the classic miscount.
Why mutually-exclusive branches. Use if (x > 0) … else if (x < 0) … else … so each element lands in exactly one bucket; overlapping conditions would double-count or miss.
Order of the result. Return the counts in the agreed order ([pos, neg, zero] here). The pattern — one counter per category, one pass — scales to any fixed set of disjoint categories.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, -2, 0, 3, -1]1
pos
[1,0,0][2, 2, 1]C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n) time, O(1) space — one three-way comparison and one increment per element.
E · Edge cases and pitfalls4nums = [0, 0] — All zeros → [0, 0, 2].Explore edge casesHide edge cases
Edge cases handled
nums = [0, 0]All zeros → [0, 0, 2].nums = [4]One positive → [1, 0, 0].
Pitfalls
- Counting zero as positive (using `>= 0`).
- Returning the buckets in the wrong order.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int[] classify(int[] nums) { int pos = 0, neg = 0, zero = 0; for (int x : nums) { if (x > 0) pos++; else if (x < 0) neg++;Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Counting zero as positive.
- Returning the buckets in the wrong order.
Revise
Carry the pattern into the interview
Pattern to remember
Three-bucket tally
Code pattern to memorize
if x>0 pos++ elif x<0 neg++ else zero++Rules to remember
- 1>0, <0, else zero.
- 2One counter per bucket.
- 3Single pass.
Use this pattern when
- Tallying a few disjoint categories
Interview takeaway
Mutually-exclusive categories → one counter each, one pass.
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: Three-bucket tally. Master it on small inputs and it transfers to harder problems.