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

Binary Search on the Answer

Binary Search on the Answer 30 minWhen a yes/no test flips only once, search the answers instead of the data

dsa / Binary Search on the Answer

Explore each scene at your own pace.

01 · You've done this once already

You've done this once already

Binary Search on the Answer

Definition
Binary search on the answer finds the smallest or largest value that passes a yes-or-no feasibility test, when the test turns from no to yes exactly once as the value grows. Each check halves the range of possible answers.
In plain words
It is the guess-my-number game applied to a quantity such as shard size. Ask would this size work, and each reply rules out half of the sizes that remain. A billion possibilities take about 30 questions.

In short: when "is this value good enough?" flips from no to yes exactly once as the value grows, you can binary search the answer itself, not just a sorted list. Pick a range you know contains the answer, write a yes/no feasibility check, prove in one sentence that it's monotone, then halve the range with the usual template. A range of a billion costs about 30 checks — the search is never the bottleneck, the check is.

Four probes on the range 1..30: 15 fits (keep 1..15), 8 fails (keep 9..15), 12 fails (keep 13..15), 14 fails, and lo meets hi at 15

Quarry splits every table into shards, and each shard holds at most c rows. A table with r rows therefore needs ⌈r/c⌉\lceil r / c \rceil shards, and the cluster has room for a fixed budget of shards across all tables. Big shards are slow to move and rebalance, so the capacity tuner wants the smallest c that still fits every table into the budget.

python

There's no sorted list here to search, and there's no formula that gives c directly, because the ceilings get in the way. But look at the output. As c grows, the shard count never goes up. So the question "does capacity c fit in 6 shards?" reads no, no, no, …, yes, yes, yes as c increases. It flips exactly once. Anything that flips exactly once can be binary searched.

You've done this once already

In the binary search lesson, grid_side found the biggest square grid of album covers, and first_true found the first day a track crossed a million plays. Neither searched a list. Both searched a range of possible answers with a yes/no test. This lesson turns that trick into a method you can apply on purpose. It has four questions, and you should answer them in a comment before writing the loop:

  1. What is the answer space? Pick lo and hi so that the answer is certainly inside. hi must be a value you know works. Here c = max(tables) puts every table in one shard, so if any capacity fits, that one does.
  2. What is the test? A function fits(c) that says yes or no. This is often called the feasibility check, and it's usually a simple greedy or counting loop.
  3. Is it monotone, and which way? No-then-yes means you want the first yes (a smallest capacity). Yes-then-no means you want the last yes (a largest gap, a biggest grid).
  4. What does one test cost? The search makes about log⁡2R\log_2 R tests over a range of size RR, so the total is O(log⁡R×test)O(\log R \times \text{test}).
Scene 1 of 15
Project context and further resources
You're building a piece ofQuarry — the query planner
This piece — min_capacity(): The smallest shard capacity that fits every table into the shard budget.
Scenario The cluster has room for six shards and four tables to hold. Quarry's capacity tuner finds the smallest shard size that makes everything fit, in a handful of probes instead of trying every size.
min_capacity.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
min_capacity(tables, budget) → intThe smallest shard capacity that fits every table into the shard budget, or -1.
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.