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

Lesson 5.9 of 10 in this module Unrolled Pro

Median of Two Sorted Arrays

The median of two sorted arrays combined, in O(log(min(m, n))). From concatenating and sorting, to a two-pointer merge that stops halfway (O(m + n)), to a binary search over where to cut the smaller array so that everything on the left is ≤ everything on the right. Then the k-th smallest element, the median latency across many sorted shards by searching on the value, a streaming median, and approximate quantiles at scale.

about 10 min 4 Python examples 2 quick checks

Problem. Given two sorted arrays a (length m) and b (length n), return the median of all m + n values, in O(log(m + n)).

text
a = [1, 3],    b = [2]        →   2.0
a = [1, 2],    b = [3, 4]     →   2.5     ((2 + 3) / 2)
a = [],        b = [5]        →   5.0

The median splits the combined values into a left half and a right half of equal size (the left gets one extra when the total is odd), with every left value ≤ every right value. We never need to build the combined array: we only need to find where that split falls in each array.

ML context. Medians and percentiles are the honest summaries of latency and loss (a mean hides the slow tail). Logs are often already sorted per shard: per-server latencies, per-day scores. Combining two sorted summaries into one percentile without merging them is this problem; across many shards it becomes a search on the value (Change B), and at real scale an approximate sketch (Change D).


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 "Median of Two Sorted Arrays"

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