VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 09: Heaps and top-k

Lesson 9.8 of 12 in this module Unrolled Pro

Find Median from Data Stream

Add numbers one at a time and report the median at any moment. From sorting on every query (O(n log n) per median), to keeping a sorted list with binary insertion (O(n) per add because of shifting), to two heaps, a max-heap for the lower half and a min-heap for the upper half, balanced so the median sits on top (O(log n) add, O(1) median). Then a sliding window with lazy deletion, any percentile by changing the split, bounded integer values with counts, and approximate quantiles at scale.

about 10 min 4 Python examples 2 quick checks

Problem. Design MedianFinder with add_num(x) and find_median(). The median is the middle value of all numbers added so far, or the mean of the two middle values when the count is even.

text
add 1, add 2     → median 1.5
add 3            → median 2.0

Lesson 5.9 found the median of two static sorted arrays. Here the data arrives, and each query must be cheap. The trick is that the median only needs the boundary between the lower and upper halves, never the full order inside them.

ML context. Medians and percentiles are the honest summaries of skewed quantities: request latency, tokens per response, gradient norms, per-batch loss (a mean is dragged by one huge outlier). Monitoring computes them online as samples stream in, usually over a sliding window (Change A) and for several percentiles (Change B), and at fleet scale approximately (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 "Find Median from Data Stream"

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