Rolling Median with Two Heaps

medium
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 22. Everything before it counts as cleared.

A rolling median is robust to spikes, but recomputing it from scratch is slow. Implement rolling_median(x, k) returning medians for each full window x[i : i + k] (length n - k + 1).

A simple correct approach keeps a sorted list of the current window and uses bisect to insert the new value and remove the old one, giving O(k) per step. (The two-heap approach with lazy deletion is O(log k) and a good extension.)

#algorithms#time-series#robust-statistics2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Rolling Median with Two Heaps · TechGradify