cs-035 — The Longest Streak
| Field | Value |
|---|---|
| id | cs-035 |
| difficulty | Intermediate |
| topics | aggregation, state |
| lecture | lecture-09 |
| estimated time | 5-minute attempt + 10-minute discussion |
| prerequisites | loops, lists, and tracing from lectures 1-6 |
| objective | choose the technique, trace it faithfully, and defend the boundary behaviour |
| solution | held by the instructor; discussed after the attempt |
Problem
Count the longest run of consecutive goals met in a habit tracker. Two-level state: current streak vs best streak, and resetting correctly.
Your deliverable is the computed aggregates for the worked instance, the accumulator update rules, and one test input that breaks a careless initialisation. If the problem is ambiguous anywhere, state the assumption you are making and carry on — a stated assumption is part of a good answer.
Examples
- Input: goals met: [1, 1, 0, 1, 1, 1, 0, 1]
- Output: current best streak 3 (days 4-6); final streak at end is 1 - do not confuse the two
Solve for the worked instance first. Then ask what would change if one number, name, or condition were replaced by something stranger.
What to notice
- Two counters: current streak and best streak. Reset one, update the other, in the right order.
- Every aggregate here is a small machine: state + update rule.
- The initial value is part of the algorithm, not a detail.
Discussion starter
After the solution is revealed, the class spends five minutes on this prompt: "Which accumulator was hardest to get right, and what test would have caught the mistake?"
Constraints and assumptions
- Difficulty: Intermediate (loops and lists assumed).
- Topics: aggregation, state.
- Classroom-scale inputs unless the problem says otherwise.
- Time-box: about five minutes for the plan and trace; finishing the details is a bonus, defending the method is the target.
- Optional stretch (not required): describe an alternative approach and say when it beats the obvious one.
Hints and extension
- First nudge: identify the pattern the worked instance uses before computing anything.
- If stuck after three minutes, write the input/output line you expect and trace the smallest example by hand.
- After the reveal, attempt the extension below before the next lecture.
- Extension challenge: Recompute all aggregates in a second pass and argue which pass you would keep.