Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Thinking in Patterns  ›  Lesson

Patterns: Reusable Moves and Their Signals

What are coding patterns? 12 minA coding pattern is a reusable shape of solution, and a few words in a problem statement usually signal which pattern fits.

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.

  1. Does it want every option, or one answer? Every option, with tiny limits, means backtracking.
  2. Is the answer a contiguous stretch? Then think sliding window, or prefix sums for range totals.
  3. 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.

Classifying a delivery-log problemStep 1 of 4
  1. 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.

quick drill

"List every way to pick a team of 3 from 8 volunteers." Which pattern?

quick drill

"Do any two badge numbers in this unsorted list match?" Which pattern?

quick drill

"Answer 10,000 questions of the form: total rainfall from day a to day b." Which pattern?

quick drill

"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?

quick drill

"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.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainInvent a problem, point to its signal

Pick two different patterns from the table. For each, write a one-sentence problem of your own that needs it, then point to the exact words in your problem that give the pattern away and explain why those words fit.