Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Keep `current` and `best`. On a 1, increment `current` and update `best`; on a 0, reset `current` to 0. O(n) time, O(1) space.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you maintain current/best counters and reset correctly.
Where candidates slip
No reset on 0, or updating best too late.
How to stand out
Connect it to the sliding-window 'max ones with k flips' 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
Running streak
1Hint 1Explore hintHide hint
Keep the length of the current run and the best run so far.
2Hint 2Explore hintHide hint
What happens to the current run when you hit a 0?
3Hint 3Explore hintHide hint
Update best every time the current run grows, not just at the end.
A · Core idea
Track two counters in one pass: current (the length of the run you're in) and best (the longest run seen). On a 1, grow current and refresh best; on a 0, reset current to 0.
Why reset on 0. A run of ones is broken the instant a 0 appears, so the current streak must restart from zero. Failing to reset is the classic bug — you'd count ones that aren't actually consecutive.
Why update `best` inside the loop. The longest run might end in the middle of the array (not at the last element), so you must capture best every time current grows, not just once at the end.
This running-streak pattern (a current counter that resets on a breaker, plus a best counter) generalises to longest run of any property and is a stepping stone to sliding-window problems like 'max ones after flipping k zeros'.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, 1, 0, 1, 1, 1]1
current=1
best=13C · 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 increment-or-reset and at most one max-update per element.
E · Edge cases and pitfalls5nums = [0, 0] — No ones → 0.Explore edge casesHide edge cases
Edge cases handled
nums = [0, 0]No ones → 0.nums = [1, 1, 1]All ones → 3.nums = [1, 0, 1]Best run is 1.
Pitfalls
- Forgetting to reset the streak on a 0.
- Updating best only after the loop, missing a run that ends mid-array.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int maxOnes(int[] nums) { int best = 0, current = 0; for (int x : nums) { if (x == 1) { current++;Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Forgetting to reset the streak on a 0.
- Updating best only at the end instead of inside the loop.
Revise
Carry the pattern into the interview
Pattern to remember
Running streak / reset
Code pattern to memorize
cur=0,best=0; for x: cur = x==1 ? cur+1 : 0; best=max(best,cur)Rules to remember
- 1Grow on 1, reset on 0.
- 2Update best while growing.
- 3O(1) state.
Use this pattern when
- Longest run of a property
- Streak tracking
Interview takeaway
Longest-run problems need a current counter and a best counter.
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: Running streak / reset. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Max Consecutive Ones IIIComing soonSliding window allowing k zero-flips.