Problem Solving — CS & DS

Lecture 09 — Aggregation Patterns

Learning objectives

Case-based lecture: you attempt, we discuss, then we reveal.

Today

The plan

Key definitions

Key definitions

Explain each in one breath; the cases will do the heavy lifting.
Worked example

The reading log: one pass, four aggregates

cs-033

Pages [12, 0, 25, 8, 30], target 10. Report total, average, best day, and how many days fell below target.

Expected: {'total': 75, 'avg': 15.0, 'best': 30, 'below': 2}

Cost: O(n) time, O(1) space - four aggregates for the price of one loop.

Worked example

Stockroom spikes: argmax with witnesses

cs-034

Sales [100, 210, 205, 420, 90]. A day is a spike if it beats both neighbours. Reference answer: [1, 3].

Expected: [1, 3]

Cost: O(n) time, O(1) extra space (output excluded).

Worked example

The longest streak: reset vs stop

cs-035

Goals met [1, 1, 0, 1, 1, 1, 0, 1]. Longest streak of consecutive met goals - plus two zero cases: end-on-a-miss, and all-miss.

Expected: best 3 | end-current 0 in the reference's variant | all-miss 0

Cost: O(n) time, O(1) space - the two-variable discipline is the whole lesson.

Worked example

Rainfall windows: the slide

cs-036

Rain [3, 5, 2, 8, 1, 4], window k = 3. The reference prints [5, 8, 8, 8] - derive what question it actually answers before trusting the prose.

pages = [12, 0, 25, 8, 30]
target = 10
total = sum(pages)
best = max(pages)
below = sum(1 for p in pages if p < target)
print(total, total / len(pages), best, below)

Expected: sums [10, 15, 11, 13]; maxima [5, 8, 8, 8] (the reference's question)

Cost: O(n) slide vs O(n*k) recomputed - the analysis teaser for Block III.

Case attempt · lecture 09
Case cs-033 · Beginner

The Reading Log

From a list of daily page counts, output total, average, best day, and days below target. The canonical running-total lecture case.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-033 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-033 — not embedded in this deck.

Case attempt · lecture 09
Case cs-142 · Beginner

The Tallied Vote

Eleven committee members each wrote one name on a slip:

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-142 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-142 — not embedded in this deck.

Case attempt · lecture 09
Case cs-034 · Foundational

Stockroom Spikes

Find days when sales exceeded double the previous day's sales. Combines aggregation with a 'look at the previous element' pattern.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-034 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-034 — not embedded in this deck.

Case attempt · lecture 09
Case cs-035 · Intermediate

The Longest Streak

Count the longest run of consecutive goals met in a habit tracker. Two-level state: current streak vs best streak, and resetting correctly.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-035 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-035 — not embedded in this deck.

Case attempt · lecture 09
Case cs-036 · Advanced

Rainfall Windows

For a window of the last k days, report the running maximum without re-summing the whole window each time. A gentle sliding-window preview.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-036 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-036 — not embedded in this deck.

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. Name the aggregation pattern for 'which day had the most sales'.
  2. Why reset streak state?
  3. What is two-pass thinking and when does it pay?
Expected answers (instructor): argmax: track best-so-far value and its index/key a streak is consecutive; a miss must zero the counter, not stop it compute intermediate aggregates first, then answer; pays when one pass tangles state
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF