Patterns: Reusable Moves and Their Signals
There are thousands of practice problems, but only a dozen or so moves behind most of them. The people who seem to "just see" a solution are not faster at inventing. They have learned which few words in a problem point to which move, and they reach for it straight away.
The rule
Memorise moves and their signals, not individual problems.
A pattern is a reusable shape of solution with a known cost. Once you know the shape, a new problem becomes "which shape is this?" rather than "invent something from nothing". This course teaches six:
| Signal in the problem | Pattern | Usual cost |
|---|---|---|
| "seen before?", "a pair that adds to", "count", "group" | hash set or dictionary | one pass, extra memory |
| "total of a range", asked many times | prefix sums | one pass, then instant ranges |
| sorted input, pairs or triples, compare both ends | two pointers | one pass, no extra memory |
| sorted input, or "the smallest value that still works" | binary search | about log n checks |
| "consecutive", "substring", "longest or shortest stretch" | sliding window | one pass |
| "list all", "every combination", tiny limits | backtracking | exponential, fine for small n |
Two cautions. First, signals are hints, not proofs: you still check that the move really fits. Second, real problems often combine moves, such as a sliding window that keeps its counts in a dictionary.
The shortcut
Ask three questions, in this order, and stop at the first yes.
- Does it want every option, or one answer? Every option, with tiny limits, means backtracking.
- Is the answer a contiguous stretch? Then think sliding window, or prefix sums for range totals.
- Is the data sorted, or can I sort it without losing what is asked? Then think two pointers or binary search.
If all three are no, you are probably asking "have I seen this before?" over and over, and a set or dictionary answers that instantly.
The problem
Parcels arrive in order, each with a weight. Find the longest run of consecutive parcels whose total weight is at most 50 kg.
The trap is jumping at the first familiar word. "Sorted" appears in many problems, but if the question is about consecutive items in arrival order, sorting would scramble the very thing you were asked about.
After every problem you solve, say the pattern's name out loud. That habit is how the signals become automatic.
Drills
Answer fast — trust the rule.
"List every way to pick a team of 3 from 8 volunteers." Which pattern?
"Do any two badge numbers in this unsorted list match?" Which pattern?
"Answer 10,000 questions of the form: total rainfall from day a to day b." Which pattern?
"Find the lowest oven temperature at which every loaf bakes within an hour. A helper tells you yes or no for any temperature." Which pattern?
"Parcels arrive in order. Find the longest run of consecutive parcels under 50 kg in total." Why is sorting the weights first a mistake?
Remember
- Memorise moves and their signals, not individual problems.
- Three questions, in order: every option? contiguous stretch? sorted? Otherwise, a lookup.
- Trap: grabbing the first familiar word, such as sorting data whose order matters.
