Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Backtracking  ›  Capstone Project

Capstone: Which Pattern Is This?

More Exercises 18 minBefore coding, climb a short ladder of signals (list every option, a contiguous stretch, sorted data, the smallest value that works, a lookup) and check the chosen pattern against the input limits.

Capstone: Which Pattern Is This?

You now own a small toolbox: seen-sets, complement lookups, counting, canonical keys, prefix sums, two pointers, binary search on lists and on answers, fixed and variable windows, and backtracking. In an interview nobody tells you which tool the problem wants. This final lesson is the habit that picks one in the first minute, before any code exists.

The rule

Name the pattern before you write a single line of code.

The first lesson of this course gave three questions. With everything since, they grow into a recognition ladder. Climb it from the top and stop at the first rung that fits:

Rung Signal Reach for
1 "list every", "all ways", tiny limits backtracking: subsets, orderings or pruned combinations
2 "contiguous", "stretch", "substring" sliding window, or prefix sums if values can be negative or ranges are asked many times
3 sorted data, pairs or triples, "both ends" two pointers, or binary search to locate a value or a boundary
4 "the smallest (or largest) x that still works" binary search on the answer, with a yes-no checker
5 "seen before", "partner", "how many of each", "group" set, dictionary, counts or canonical keys

Then check the fit against the limits, as in the very first lesson. A pattern that is too slow for the largest input, or that needs sorted data you are not allowed to sort, is the wrong rung: climb on.

Two habits make the ladder reliable. Say why the tempting rung fails, not just why the chosen one fits: "not a window, because values can be negative" is exactly the reasoning interviewers hope to hear. And remember that patterns combine: a window that keeps a dictionary of counts, prefix sums that lean on a complement lookup, binary search whose checker is a greedy pass.

The shortcut

Read the problem once for its story and once, slowly, for its signal words and limits. Underline them. The rung usually announces itself.

A fresh problem on the ladderStep 1 of 4
  1. The problem

    A game logs each round's score change in order, and penalties make some changes negative. Up to 100,000 rounds. How many stretches of consecutive rounds add up to exactly 50?

Drills

Answer fast — trust the rule.

quick drill

"A sorted list of house numbers: find two that add up to 1,000, using no extra memory." Which pattern?

quick drill

"Find the smallest battery size that lets a drone finish its route with at most 5 recharges. A helper says yes or no for any size." Which pattern?

quick drill

"The longest stretch of a gene string that contains at most 2 different letters." Which pattern?

quick drill

"List every order in which 5 guests can be seated in a row." Which pattern?

quick drill

"An unsorted list of 100,000 usernames: find two that are the same apart from capital letters." Which pattern?

Remember

  • Name the pattern before you write a single line of code.
  • Shortcut: climb the ladder (every, contiguous, sorted, smallest that works, seen), then check the limits.
  • Trap: stopping at a tempting rung without saying why the others fail.

Practice — level up

0/1 solved

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

ExplainClimb the ladder twice

Classify each problem with the recognition ladder. Name the pattern, quote the signal words, say why a tempting alternative fails, and estimate the cost. (a) Up to 100,000 daily temperatures and 50,000 questions of the form "what was the average temperature from day a to day b?" (b) Up to 12 tasks with known durations: list every way to split them between two workers so that both work exactly the same total time.