Sliding Window Median of API Response Latencies
Last updated: November 29, 2026
Quick Overview
Given a stream of response latencies and a window size k, return the median of each window of k consecutive values as the window slides one step at a time.
0
0
0 solved
Given a stream of response latencies and a window size k, return the median of each window of k consecutive values as the window slides one step at a time.
LinkedIn's services report latency percentiles over rolling windows, and the median is the simplest robust one. The onsite interviewer expects two heaps with lazy deletion or a sorted container, and a clear balance invariant.
What the Interviewer Expects
- Uses a max-heap for the lower half and a min-heap for the upper half
- Keeps the invariant that the lower half has k//2 + k%2 valid elements
- Removes outgoing elements lazily with a delayed-deletion counter
- Prunes stale heap tops before reading the median
- 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
- How would you compute p99 instead of the median over a window?
- What changes if the window is time-based (last 60 seconds) rather than count-based?
- How would you merge window medians from many hosts?
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 nums and 1 <= k <= len(nums). Output: list of n - k + 1 medians. For even k, the median is the average of the two middle values, returned as a float....
High-Level Design
Two heaps: lo is a max-heap (stored negated) and hi a min-heap. Invariant: valid sizes satisfy size_lo == size_hi or size_lo == size_hi + 1, and every value in lo is at most every value in hi....