Lecture 09 — Aggregation Patterns

Position in the course

This is lecture 9 of 32. It belongs to the course arc described in the course outline and uses the standard interactive case method: display, attempt (5 minutes), discuss, reveal, name the technique.

Learning objectives

By the end of this lecture students can do all three under time pressure, which is what the four cases exercise.

Prerequisite knowledge

Before this lecture: Lectures 04–06 — Loops, state, accumulation

Assumed fluencies:

Preparation task: Write a running-total loop for cs-033 from memory.

Detailed topic outline

Core (mandatory):

  1. running totals, counts, min/max, argmax
  2. comparison against a best-so-far
  3. streaks and state resets
  4. fixed and variable windows

Extension (optional enrichment):

  1. two-pass thinking
  2. choosing the right aggregation for a question

Case sequence

CaseTitleDifficulty
cs-033The Reading LogBeginner
cs-142The Tallied VoteBeginner
cs-034Stockroom SpikesFoundational
cs-035The Longest StreakIntermediate
cs-036Rainfall WindowsAdvanced

Display each case full-screen from the student page (cases cs-033–cs-036); instructor pages cs-033–cs-036 under case-studies/instructor/ hold the full solutions, reveal guides, and discussion prompts. Applied additions cs-142 extend the session as optional extra attempts or homework; they share the lecture's theme but teach an applied, real-world angle.

Timing plan (120 minutes)

MinutesSegment
0–10Framing: where this fits; recall of the previous lecture
10–30Case 1 (cs-033): 5-minute attempt, then discussion and reveal
30–50Case 2 (cs-034): same rhythm
50–70Case 3 (cs-035): same rhythm, harder case
70–75Break
75–100Case 4 (cs-036): same rhythm, hardest case
100–115Synthesis: name the techniques; misconceptions check
115–120Exit questions, homework, next-lecture preview

Timing flexes with the room: if a case ignites the class, let the discussion run and compress the synthesis; if the attempts stall, harvest partial attempts rather than extending the clock.

Worked examples

Instructor demonstration to open the lecture: Compute the reading-log totals (cs-033) with running totals, then re-ask the question with min/max to show the pattern family.

Classroom activities

#ActivityMinutesFormat and prompt
1Running-total simulation6One student reads numbers aloud, one keeps the total, one keeps the max; swap roles mid-stream.
2Window slide demo7A meter-stick slides along a taped number line of rainfall values; the class calls out each window sum.
3Pattern naming5Six aggregation questions are shown; teams name the pattern (total, count, min, argmax, streak, window) before solving.

Board plan

Instructor explanation notes

Reading the room. Aggregation patterns are the workhorses of data science; name them precisely (total, count, min, max, argmax, streak, window) so later lectures can invoke them by name.

Running the reveal. The window cases (cs-036) are the hardest; two-pass thinking is the accessible route and is worth celebrating.

Lecture focus. One sentence to repeat verbatim: Name the pattern, then write the loop.

Common misconceptions

Each case's instructor page lists the specific wrong turn to expect; the reveal should address it by name.

Student practice exercises

Core:

  1. Implement running total, count, min, and argmax over one provided reading log.
  2. Compute the longest streak of on-target days for cs-035's sample data by hand, then in code.
  3. Solve cs-036 for window size 3 with the two-pass method; then attempt one pass.

Enrichment (optional):

Formative assessment

Exit questions (choose one; answer on a card in 60 seconds):

  1. Name the aggregation pattern for 'which day had the most sales'. Expected: argmax: track best-so-far value and its index/key
  1. Why reset streak state? Expected: a streak is consecutive; a miss must zero the counter, not stop it
  1. What is two-pass thinking and when does it pay? Expected: compute intermediate aggregates first, then answer; pays when one pass tangles state

Cards are skimmed before the next lecture; three answers become the framing. Persistent wrong answers route students to the review stations of lecture 16 or to office hours.

Homework and preparation

Learning-outcome mapping

Differentiation