Median Request Latency Over a Sliding Window of Calls

Last updated: November 29, 2026

Quick Overview

Given a sequence of request latencies and a window size k, return the median latency for each window of k consecutive requests (average of the two middle values when k is even), faster than re-sorting each window.

OpenAI
Coding & Algorithms
Software Engineer
OpenAI
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given a sequence of request latencies and a window size k, return the median latency for each window of k consecutive requests (average of the two middle values when k is even), faster than re-sorting each window.

Latency medians and percentiles over recent requests drive alerts for OpenAI's API, and a sliding median is the classic hard version of that computation. Interviewers want two heaps with lazy deletion, a clearly stated balance invariant, and careful size bookkeeping since heap lengths no longer equal the number of live elements.

What the Interviewer Expects
  • Use a max-heap for the lower half and a min-heap for the upper half
  • State and maintain the size invariant using live counts, not heap lengths
  • Implement lazy deletion with a pending-removal map and prune heap tops
  • Decide which half a removed value belongs to by comparing with the lower top
  • Validate against a brute-force median on random inputs
  • Mention a sorted container or order-statistic tree as an alternative
Key Topics to Cover
heaps
two heaps
lazy deletion
sliding window
order statistics
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 would you use?
  • The stream is unbounded and you need approximate percentiles in constant memory: which sketches apply?
  • Windows are defined by time (last 10 seconds) with variable counts: what changes?
  • Compute medians per model and per region at once.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: list of numbers and k, with 1 <= k <= n (otherwise return []).
  • Output: list of n - k + 1 medians as floats.
  • Even k: average of the two middle values.
  • Target better than $O(nk ...
High-Level Design

Split the window into a lower half (max-heap) and an upper half (min-heap) so that every lower value is at most every upper value and the lower half has the same size or one more. The median is the lo...


Submit Your Answer
Markdown supported