VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 05: Binary search

Lesson 5.1 of 10 in this module Unrolled Pro

Binary Search, and Searching on the Answer

Binary search finds the boundary where a yes/no question flips from no to yes, in O(log n) steps. From a linear scan, to halving with one invariant and one template that avoids off-by-one bugs, to searching on the answer itself: the smallest value that works. Then expensive checks (finding the first bad checkpoint), real-valued answers, and when a hash map beats a binary search.

about 10 min 5 Python examples 2 quick checks

The question this module answers: where does the answer flip? Many questions have the shape "the first index where something becomes true": the first value ≥ x in a sorted list, the first checkpoint whose eval got worse, the smallest batch size that runs out of memory. If the yes/no question is monotone (all no, then all yes), you never need to look at every position. Ask in the middle, throw half away, repeat.

ML context. Binary search hides inside a lot of ML tooling:

  • Thresholds and quantiles: np.searchsorted and torch.bucketize place values into sorted bin edges.
  • Regressions: "which of 64 checkpoints first broke the eval?" is answered with 6 evaluations, not 64.
  • Capacity: the largest batch size or sequence length that fits in GPU memory (lesson 5.10).
  • Sampling: drawing from a categorical distribution searches the cumulative probabilities.

1. The problem: scanning for the boundary

Task: in a sorted list, find the first index whose value is ≥ x (or len(a) if there is none).

The obvious way walks from the left until the condition holds.

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Binary Search, and Searching on the Answer"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee