cs-034 — Stockroom Spikes
| Field | Value |
|---|---|
| id | cs-034 |
| difficulty | Foundational |
| topics | aggregation, comparison |
| lecture | lecture-09 |
| estimated time | 5-minute attempt + 10-minute discussion |
| prerequisites | lectures 1-2 (problem framing, precise pseudocode) |
| objective | recognise the pattern, translate it into precise steps, and trace it correctly |
| solution | held by the instructor; discussed after the attempt |
Problem
Find days when sales exceeded double the previous day's sales. Combines aggregation with a 'look at the previous element' pattern.
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: sales [100, 210, 205, 420, 90]
- Output: spike days: index 1 (210 > 2x100) and index 3 (420 > 2x205); not index 2 (205 < 2x210)
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
- Keep the previous day's value as state; compare each day to 2x previous - the first day has no previous.
- 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: "Where exactly would this solution break first if the input grew to a million items — and would anything break at all?"
Constraints and assumptions
- Difficulty: Foundational (early-semester techniques assumed: precise statements, traces, simple loops).
- Topics: aggregation, comparison.
- Classroom-scale inputs unless the problem says otherwise.
- Time-box: about five minutes; naming the pattern and stating the assumption is already a pass, completing the calculation is the stretch.
- 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.