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

Lesson 5.2 of 10 in this module Unrolled Pro

Binary Search

Find a target's index in a sorted array, or −1. From a linear scan (O(n)), to a recursive search that slices the list (looks O(log n), is really O(n) because slices copy), to an iterative search on indices (O(log n) time, O(1) space). Then many queries, membership-only lookups where a hash set wins, an array of unknown size (exponential search), and lookups in a sorted id table.

about 10 min 4 Python examples 2 quick checks

Problem. Given a sorted array of distinct integers nums and a target, return the index of target, or −1 if it isn't there. The solution must run in O(log n).

text
nums = [-1, 0, 3, 5, 9, 12], target = 9   →   4
nums = [-1, 0, 3, 5, 9, 12], target = 2   →   -1

ML context. Sorted id tables are everywhere: token ids to embeddings rows, user ids to feature rows, timestamps to log offsets. Looking one up without building a hash map (which may not fit in memory, or may not exist on disk) is a binary search. np.searchsorted runs millions of these at once.


1. Step 1: Clarify

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

Unlock "Binary Search"

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