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

Lesson 5.3 of 10 in this module Unrolled Pro

Search Insert Position

Return where a target is, or where it would be inserted to keep a sorted array sorted. From a linear scan, to an exact-match search plus a fix-up, to the boundary template that returns the answer directly: the first index whose value is ≥ target. Then duplicates (left vs right insertion), many inserts, bucketizing features into bins, and finding the nearest value.

about 10 min 5 Python examples 2 quick checks

Problem. Given a sorted array of distinct integers and a target, return the index of target if it's there; otherwise return the index where it would be inserted to keep the array sorted. Must run in O(log n).

text
nums = [1, 3, 5, 6], target = 5   →   2
nums = [1, 3, 5, 6], target = 2   →   1
nums = [1, 3, 5, 6], target = 7   →   4

Both cases have one answer: the first index whose value is ≥ target. That's the boundary search from lesson 5.1.

ML context. "Where would this value go?" is how features are bucketized: an age, a price or a score is mapped to a bin by searching sorted bin edges (np.digitize, torch.bucketize, quantile binning for gradient-boosted trees). It's also how you find the nearest timestamp in a log, or the step closest to a given wall-clock time.


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 "Search Insert Position"

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