VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 14: Greedy and intervals

Lesson 14.3 of 6 in this module Unrolled Pro

Insert Interval

Insert one new interval into a sorted list of disjoint intervals, merging where needed. From appending and re-merging everything (O(n log n)), to one linear pass in three phases: intervals entirely before, intervals that overlap (absorbed into the new one), intervals entirely after (O(n)), and binary search to find the overlapping block in O(log n). Then many inserts with a sorted structure, removing a range, and a GPU reservation calendar that finds the first free slot of a given length.

about 10 min 3 Python examples 2 quick checks

Problem. intervals is sorted by start and has no overlaps. Insert new so the list stays sorted and disjoint, merging any intervals it overlaps (touching counts as overlapping).

text
intervals = [[1, 3], [6, 9]],                                new = [2, 5]   →   [[1, 5], [6, 9]]
intervals = [[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]],     new = [4, 8]   →   [[1, 2], [3, 10], [12, 16]]

Lesson 14.2 merged an unsorted list. Here the list is already sorted and clean, and only one interval is new. Re-sorting throws that information away. Instead, the new interval splits the list into three contiguous parts: intervals entirely before it, a block that overlaps it, and intervals entirely after it.

ML context. A shared GPU cluster keeps, for each machine, the sorted list of reserved time ranges. Booking a job inserts an interval; asking "when is the next free 2-hour window?" scans the gaps between reservations (Change D). The same structure tracks which byte ranges of a dataset have been downloaded, or which spans of a document have been annotated.


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 "Insert Interval"

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