Systematic designAdvanced

Running median of a stream

Scenario

Design a component that ingests a stream of numbers and can return the median of everything seen so far at any time. Ingest must be fast; queries are frequent. Then extend it to a *sliding* median over the last w values.

Your task

  1. Define the median precisely for even and odd counts.
  2. Reject the naive designs: re-sorting, insertion into a sorted list, a single heap. Give the cost of each.
  3. Present the two-heap design: invariants, insertion procedure, rebalancing, and query.
  4. Handle the sliding-window extension: what breaks, and what structure fixes it?
  5. State complexities and mention when a completely different approach (bucketed counts) is better.
Systematic ReasoningImplementationComplexity AnalysisEdge Cases

Work it out

Write your analysis before revealing anything. The self-check below compares it against what a strong answer contains.

Reveal

Progressive — each section builds on the previous one.

Key observation
The fix
Edge cases
Complexity
What this tests

Self-check

Tick what your analysis covered. Be honest — this feeds your readiness profile.

0/7

Related concepts