Skip to the workspace
Pebellslearn by building
Monotonic Structures & Search on the Answer  ›  Capstone Project

Capstone — Quarry's Query Planner

45 minThree one-pass techniques and one monotone search, combined into a report

dsa / Capstone — Quarry's Query Planner

Explore each scene at your own pace.

01 · What you're building

What you're building

Capstone — Quarry's Query Planner

Definition
A query planner here combines three techniques over one load trace: a monotonic deque for window peaks, a monotonic stack for the wait until a heavier tick, and a binary search on the answer with a simulation for the smallest sufficient capacity.
In plain words
It is a control room reading one chart three ways: what was the busiest moment in each recent stretch, how long until it gets busier, and how big a server we need to keep the queue from growing past the promise.

In short: plan_capacity reads one load trace and answers three questions, each with a technique from this section: a monotonic deque for the window peaks, a monotonic stack for the wait until the next heavier tick, and a binary search on the answer — backed by a simulation — for the smallest capacity that keeps the backlog inside its promise. Keep the three as small, separately-tested functions rather than one clever merged pass, prove the search's monotonicity by comparing two runs, and settle the edge cases (an empty trace, a window of zero, a promise of zero) before writing a line.

One load trace feeding three techniques: a monotonic deque for peaks, a monotonic stack for next_spike, and a binary search on the answer for capacity

Every piece of this section answered one question about a trace. The planner answers three at once, and it has to answer them fast enough to run on every refresh of Quarry's operations page. It reads a load trace: loads[t] is how many units of query work arrived at tick t.

What you're building

plan_capacity(loads, k, sla) -> {"peaks": [...], "next_spike": [...], "capacity": c}
plan_capacity([3, 9, 4, 7, 2], 2, 4)
# {"peaks": [9, 9, 7, 7], "next_spike": [1, 0, 1, 0, 0], "capacity": 6}
  • peaks: the heaviest load in every window of k consecutive ticks, for the dashboard. That's the monotonic deque from lesson 2. With k less than 1 or longer than the trace, it's [].
  • next_spike: for each tick, how many ticks until a strictly heavier load arrives, or 0 if none does. That's the monotonic stack from lesson 1.
  • capacity: the smallest whole number of units per tick, at least 1, that a server can process while keeping the backlog at or below sla after every tick. That's a binary search on the answer from lesson 3, with a simulation as its check.

Each part is a technique you've already written. The work in a capstone is getting their edges right, proving the one fact the search relies on, and keeping the whole thing O(n log L), where L is the heaviest load.

Scene 1 of 13
Project context and further resources
You're building a piece ofQuarry — the query planner
This piece — plan_capacity(): Reads a load trace and reports window peaks, the wait for the next spike, and the smallest capacity that meets the backlog promise.
Scenario The operations page refreshes and Quarry's planner reads the latest load trace: it shades the window peaks, marks how long each tick waited for a heavier one, and names the smallest server that would have kept the backlog promise.
plan_capacity.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
plan_capacity(loads, k, sla) → dictWindow peaks, the wait until the next heavier tick, and the smallest capacity that keeps the backlog within sla.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.