Skip to interview prep
Apexed
Problem list120 / 125

120. Running Median of a Stream

HardPriority queues & heaps

Numbers arrive one at a time. After each arrival, record the median of all numbers seen so far. Return the list of medians.

When the count is even, the median is the average of the two middle values (it may end in .5).

Example 1

Input: stream = [5, 15, 1, 3]
Output: [5, 10, 5, 4]

Constraints

  • 1 ≤ stream.length ≤ 10⁴
  • -10⁵ ≤ stream[i] ≤ 10⁵

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Running Median of a Stream — coding interview practice — Apexed