Track Rolling Median Latency Over the Last K Requests
Last updated: November 29, 2026
Quick Overview
Given a stream of request latencies and a window size k, output the median latency of the most recent k requests after each new request once the window is full.
Microsoft
November 29, 20260
0
0 solved
Given a stream of request latencies and a window size k, output the median latency of the most recent k requests after each new request once the window is full.
Azure Monitor and internal service dashboards report rolling percentiles rather than averages because a few slow calls skew the mean. Microsoft interviewers use this as a hard onsite problem to test the two-heap median pattern and, more importantly, how you remove an element that has left the window when heaps do not support arbitrary deletion.
What the Interviewer Expects
- Explains why a sorted list or re-sorting each window is too slow
- Uses two balanced heaps so the median is at the tops
- Handles expiry with lazy deletion and keeps live counts separate from heap sizes
- Maintains the invariant that heap tops are always live
- Tests against a brute-force median including 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 report p99 instead of the median?
- Latencies arrive from 10,000 hosts: how do you compute a fleet-wide rolling p50 without shipping every sample?
- Compare this with a balanced BST or order-statistic tree approach.
- What if the window is time-based (last 60 seconds) rather than count-based?
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 is a list (or stream) of numbers and a window size k. After each element from index k - 1 onward, emit the median of the last k values; for even k, the mean of the two middle values. Expected in...
High-Level Design
Split the window into a max-heap lo with the smaller half and a min-heap hi with the larger half, with lo holding the extra element when k is odd. The median is then lo's top, or the average o...