Skip to content
LeetQuant

Hard · Algorithms & Data Structures · Coding · ~25 min · #41

Streaming Median of Trade Sizes

Trade sizes arrive one at a time. After each arrival, report the median of all sizes seen so far (the mean of the two middle values when the count is even).

Implement running_median(nums: list[float]) -> list[float] returning the median after each element. Aim for O(log⁡n)O(\log n) work per element.

Examples

Input: running_median([5, 15, 1, 3])

Output: [5.0, 10.0, 5.0, 4.0]

Constraints

  • •

    1 ≤ len(nums) ≤ 10⁵

Hints · 0 of 3 shown

Worked solution

Give the problem an honest attempt first, then reveal the full explanation.

Related questions

Concepts

Two-heap techniqueStreaming order statistics

Relevant for

Quant Developer