Median of a stream

How do you maintain the running median of a stream efficiently, and at what per-insert cost?

Show hints (2)+
  1. Split the data into a low half and a high half.
  2. The median lives at the boundary of the two halves.

Answer

Reveal answer →

Two balanced heaps (max-heap low half, min-heap high half) - O(log⁡n)O(\log n) per insert

Create a free account to see the full worked solution and track your progress.

Interview format: Hedge Fund Quant, Systematic & Data-Driven Trading

Related questions