Quant Memo
Coding/●●●●

Median of a sliding window

Asked at Jane Street, Two Sigma

Prices stream in. For a fixed window size w, after the window is full report the median of the last w values as it slides forward.

nums = [1, 3, -1, -3, 5, 3], w = 3
windows: [1,3,-1] -> 1 ; [3,-1,-3] -> -1 ; [-1,-3,5] -> -1 ; [-3,5,3] -> 3
-> [1, -1, -1, 3]

Support each slide in O(logw)O(\log w). Re-sorting each window is O(wlogw)O(w \log w) per step.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions