Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Count the zeros, then fill that many 0s followed by 1s. O(n) time, O(1) space, single value-write pass.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you recognise counting sort for a tiny value set.
Where candidates slip
Using a comparison sort, or a boundary off-by-one.
How to stand out
Mention the two-pointer variant and the Dutch National Flag generalisation.
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
Count and overwrite
1Hint 1Explore hintHide hint
Only two values — do you actually need to compare anything?
2Hint 2Explore hintHide hint
Count the zeros, then write zeros-then-ones.
3Hint 3Explore hintHide hint
How would you extend this to 0s, 1s, and 2s?
A · Core idea
With only two distinct values, sorting reduces to counting. Count the zeros, then overwrite the array with that many 0s followed by 1s. O(n) time, O(1) space, beating any comparison sort.
Why counting beats comparison sort. Comparison sorts are bounded by O(n log n). But when the value set is tiny and known, you don't need comparisons — count occurrences and rewrite. This is counting sort specialised to two values.
The two-pointer alternative. A left/right pointer pair can swap 0s to the front in a single pass without a separate count — useful when you can't overwrite freely. Both are O(n)/O(1).
Generalises to three (Dutch flag). Sorting 0s/1s/2s in one pass is the Dutch National Flag problem — a natural follow-up worth naming.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, 0, 1, 0]count
two 0s
zeros=2[0, 0, 1, 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 pass to count and one to overwrite → O(n), with O(1) extra space.
E · Edge cases and pitfalls5nums = [0, 0] — Already sorted.Explore edge casesHide edge cases
Edge cases handled
nums = [0, 0]Already sorted.nums = [1]Single element.nums = [1,1,1]Zero zeros → all 1s.
Pitfalls
- Reaching for a full O(n log n) sort on binary data.
- Off-by-one on the zero-count boundary.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic void sortBinary(int[] nums) { int zeros = 0; for (int v : nums) if (v == 0) zeros++; for (int i = 0; i < nums.length; i++) { nums[i] = i < zeros ? 0 : 1;Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Reaching for a full O(n log n) sort on binary data.
- Off-by-one on the zero-count boundary.
Revise
Carry the pattern into the interview
Pattern to remember
Counting sort (2 values)
Code pattern to memorize
zeros=count(0); a[i]= i<zeros ? 0 : 1Rules to remember
- 1Count the zeros.
- 2Write 0s then 1s.
- 3O(n), no comparisons.
Use this pattern when
- Few distinct values
- Counting sort
Choose another pattern when
- Many distinct values — counting sort's range blows up
Interview takeaway
Few distinct values? Count and overwrite, don't comparison-sort.
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: Counting sort (2 values). Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Sort Colors (0s,1s,2s)Coming soonDutch National Flag — three-way one-pass partition.