Rolling Median of Fill Slippage Over the Last K Orders
Last updated: November 29, 2026
Quick Overview
Given a stream of per-order slippage values in basis points, output the median of every window of the last k values, using O(log k) work per new value.
D. E. Shaw
November 29, 20260
0
0 solved
Given a stream of per-order slippage values in basis points, output the median of every window of the last k values, using O(log k) work per new value.
A harder onsite coding question in the style of D. E. Shaw, motivated by execution analytics where a robust rolling statistic is preferred over a mean that a few bad fills can distort. The interviewer checks the two-heap design, correct removal of expiring elements, and careful size bookkeeping with duplicates.
What the Interviewer Expects
- Uses a max-heap for the lower half and a min-heap for the upper half
- Handles window expiry with lazy deletion and live-size counters
- Keeps both heap tops live so the median is always readable
- Returns correct medians for even and odd k
- Tests against a brute force with many duplicate values
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you support an arbitrary quantile such as the 90th percentile instead of the median?
- What would you use instead if values are integers in a small range?
- How would you do this with a balanced tree or sorted container, and what changes?
- How would you compute this over a time window rather than a count window?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list xs, window k. Output: list of len(xs) - k + 1 medians as floats (average of the two middle values when k is even). Must be O(n log k), since n can be millions of fills per day across...
High-Level Design
lo is a max-heap (stored negated) with the smaller half, hi a min-heap with the larger half, and we keep live(lo) equal to live(hi) or one more. Removing an arbitrary element from a heap is O(...