Rolling Median Request Latency over the Last K Requests
Last updated: November 29, 2026
Quick Overview
Given a sequence of request latencies and a window size k, return the median of each contiguous window of k latencies (the average of the two middle values when k is even).
0
0
0 solved
Given a sequence of request latencies and a window size k, return the median of each contiguous window of k latencies (the average of the two middle values when k is even).
Framed around latency dashboards for a service like Google Cloud Load Balancing, this extends the classic two-heap running median with deletions. The interviewer probes how you delete arbitrary elements from heaps (lazy deletion) and keep the halves balanced, or whether you choose an order-statistics structure instead.
What the Interviewer Expects
- Explains two heaps (max-heap low half, min-heap high half) for the median
- Handles deletion of the outgoing element via lazy deletion with a delayed-count map
- Rebalances using valid sizes rather than raw heap lengths
- Correctly averages for even k
- States O(n log k) time
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
- Return the 95th percentile instead of the median.
- The window is time-based and latencies arrive at irregular intervals.
- Memory is tight and latencies are integers in milliseconds up to 10,000; is there a simpler structure?
- Approximate percentiles across thousands of servers.
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 of numbers, integer k with 1 at most k at most n.
- Output: list of n - k + 1 medians; for even k use the mean of the two middles....
High-Level Design
Keep lo (max-heap via negation) and hi (min-heap) with the invariant that every value in lo is at most every value in hi, and valid sizes satisfy size_lo == size_hi or size_lo == size_hi + 1. ...