Capstone — Quarry's Query Planner
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.

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 ofkconsecutive ticks, for the dashboard. That's the monotonic deque from lesson 2. Withkless than 1 or longer than the trace, it's[].next_spike: for each tick, how many ticks until a strictly heavier load arrives, or0if 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 belowslaafter 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.