Tracking streaks during a traversal.
Two counters — current streak, best streak — capture any "consecutive success" pattern.
A streak is a run of consecutive elements (or events) that all satisfy some condition. Streak logic powers attendance tracking, quiz performance, hot streaks in games, and any "longest consecutive X" question.
The pattern: track a current streak that grows when the condition continues and resets when it breaks. A separate variable holds the best streak ever seen.
On the AP exam, streak logic appears in array problems and in object-list problems alike — anywhere "consecutive" or "in a row" appears in the prompt.
Increment on success; reset on failure; track the best.
Three operations cover every streak problem.
The template
int current = 0;
int best = 0;
for (int x : data) {
if (condition(x)) {
current++;
if (current > best) best = current;
} else {
current = 0;
}
}
return best;
Concrete example: longest passing streak
public int longestPassingStreak(int[] scores) {
int current = 0, best = 0;
for (int s : scores) {
if (s >= 60) {
current++;
if (current > best) best = current;
} else {
current = 0;
}
}
return best;
}
Updating best — inside the match branch only
The reset branch can never produce a new maximum, so updating best there is wasted code. Keep the update tightly bound to the success path.
Initial values
For "consecutive matches", both current and best start at 0 — a single match makes them 1.
Streak vocabulary.
Used in consecutive-success problems.
- Streak — a consecutive sequence of successes.
- Current streak — counter tracking the active run.
- Best streak — running maximum across all streaks seen.
- Reset — return current to 0 on a break.
- Match branch — code path when the condition holds.
- Break branch — code path when the condition fails.
How streak logic is tested.
Initialization and where best updates dominate.
- Update
bestinside the success branch. The reset branch can't beat best. - Reset to 0, not -1. The next success will produce 1.
- One pass. Streak logic is always single-pass.
- Both counters initialize to 0. Handles empty input gracefully.
Longest passing streak.
Trace both counters every step.
Input: [55, 70, 80, 40, 90, 85, 60, 30] with passing = 60.
Trace.
- 55 (fail): current=0, best=0.
- 70: current=1, best=1.
- 80: current=2, best=2.
- 40 (fail): current=0.
- 90: current=1.
- 85: current=2.
- 60: current=3, best=3.
- 30 (fail): current=0.
Answer: 3.
Streak pitfalls.
Most miss the best-update or reset incorrectly.
- Updating best after the loop only. Misses streaks that don't reach the end.
- Resetting to 1 instead of 0. Off-by-one — the next match would jump to 2.
- Comparing wrong direction. Use
current > bestfor strict improvement. - Forgetting to reset. Streak runs through breaks.
Streak operations.
Three actions, two counters.
Streak Reference
AP Quick Reference| Action | When | Effect |
|---|---|---|
| Increment current | Match | current++ |
| Update best | Match, if exceeds | best = current |
| Reset current | Break | current = 0 |
| Initial values | Before loop | Both 0 |
How to master streak logic.
Trace both counters; never update best in reset.
- MCQ Practice — write current and best after each element.
- Java Lab — implement longestPassing, longestPositive, longestCorrect. Vary the match condition.
- FRQ Practice — when "consecutive" or "in a row" appears, reach for this template.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 09 Topic 01 Consecutive-Run (Streak) Logic FRQ Practice
AP-style topic practice assessment