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 . Re-sorting each window is per step.
Your answer
This one is open-ended. Work it through, then check your reasoning against the full solution.