Course outline · 0% complete

0/29 lessons0%

Course overview →

The Decision Framework

lesson 10-1 · ~11 min · 28/29

One question decides everything

Every structure in this course answered the same prompt: which operations must be fast? So choosing a structure is not memorization, it is translation. Take the problem, list the operations it performs constantly, and match them:

  1. Name the hot operations. "Look up by ID", "always take the most urgent", "serve in arrival order", "read by position".
  2. Name the required orders. Arrival order? Sorted order? Priority order? Nesting? A network?
  3. Match against the toolbox, and only accept a structure whose weak operations are ones you rarely do.

The toolbox on one screen

you constantly needreach forwhy
read by positionlist (array)O(1) index formula (2-1)
grow at the endlistamortized O(1) append (2-2)
lookup / dedupe by keydict / setO(1) hashing (6-1)
newest firstlist as stackLIFO, O(1) both ops (5-1)
oldest firstdequeFIFO, O(1) both ends (5-2)
most urgent firstheapqO(log n) push/pop, O(1) peek (8-1)
sorted order + fast insertsbalanced BSTO(log n) everything (7-3)
fewest-steps between thingsgraph + BFSring-by-ring search (5-3, 9-2)
reachability / cluster countsgraph + DFSstack-driven coverage (9-3)
totals over every consecutive rangesliding windowitems enter once, leave once (3-3)
O(1) splice with node in handlinked listpointer rewiring (4-2)

Two structures barely appearing in the left column, arrays and linked lists, are still everywhere: they are what the others are BUILT FROM (hash buckets are arrays, deques are linked blocks, and a heap is an array whose index arithmetic — children at 2i+1 and 2i+2 — encodes the whole tree, lesson 8-3).

what must be fast?lookup by keydict / setread by positionlistnewest firststackoldest firstdequemost urgent firstheapsorted + insertsbalanced BSTconnections/pathsgraph + BFS
The whole course as one decision: name the operation that must be fast, then follow the arrow.

Quiz

A music app needs: skip to the next song, go back to the previous song, and insert a song right after the current one, all O(1), given the current song in hand. Which structure?

Quiz

A leaderboard shows the top 10 of 2 million players and receives thousands of new scores per second. Hot operations: insert a score, read the current top 10. Which fits best?

Problem

A web server must ensure each API key makes at most 100 requests per minute. The hot operation is "given this key string, find its request count, fast, millions of times". Which structure is the core of this rate limiter?

Quiz

The honest tiebreaker: two structures both handle your hot operations in O(1)-ish time. Lesson 4-3 gave a reason the ARRAY-BASED one usually wins in practice. What was it?