Rolling Median of Ad Request Latency Over a K-Request Window
Last updated: November 29, 2026
Quick Overview
Given a list of ad request latencies and a window size k, return the median latency of every window of k consecutive requests.
Meta
November 29, 20260
0
0 solved
Given a list of ad request latencies and a window size k, return the median latency of every window of k consecutive requests.
A Hard sliding window problem tied to latency monitoring for Meta's ad serving. The interviewer checks that you can keep two heaps balanced while elements leave the window, which requires lazy deletion since heaps do not support removing arbitrary items.
What the Interviewer Expects
- Explains why sorting each window, O(n k log k), is too slow at scale
- Uses a max-heap for the lower half and a min-heap for the upper half
- Handles removal of the outgoing element with lazy deletion and a pending map
- Keeps the halves balanced by counting valid elements, not raw heap sizes
- Gives O(n log n) time and discusses a sorted container alternative
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
- Report the 99th percentile instead of the median. What structure fits better?
- Latencies arrive as an unbounded stream and memory is fixed. How do you approximate?
- How would you compute this per ad server across a fleet of thousands?
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: lat, a list of numbers, and 1 <= k <= len(lat). Output: a list of n - k + 1 medians. For even k, the median is the mean of the two middle values (returned as a float); for odd k it is the...
High-Level Design
lo is a max-heap (stored negated) holding the smaller half, hi a min-heap holding the larger half, with lo allowed one extra valid element. Insert by comparing with the top of lo. To remove th...