Back to Practice
#0284
Running Median Stream
HardDSA40 min25 XP
Problem
Given nums as a stream, return a list of medians after each insertion. Use floats for .5 medians.
Why This Matters
Running median is the strongest everyday use case for two heaps.
Function Signature
def running_medians(nums):
Examples
Example 1
Inputnums = [5, 15, 1, 3]
Output[5.0, 10.0, 5.0, 4.0]
The sorted prefixes have medians 5, 10, 5, and 4.
Constraints
- Return the exact requested value.
- Handle edge cases cleanly.
- Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Four inserts
Input[5,15,1,3]
Expected[5.0, 10.0, 5.0, 4.0]
Each median comes from the two heap tops.
Increasing stream
Input[1,2,3]
Expected[1.0, 1.5, 2.0]
Even prefixes average the two middle values.
Hidden Test Categories
negative valuesduplicatessingle item