Rolling Median Ad Delivery Latency Over a Time-Based Window

Last updated: November 29, 2026

Quick Overview

Given ad delivery latency samples with irregular timestamps, compute for each sample the median of all latencies whose timestamps fall in the trailing window (ts - window, ts]. The window is defined in seconds, not in number of samples.

Meta
Data Manipulation (SQL/Python)
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Data Manipulation (SQL/Python)
Hard

0

0

0 solved


Given ad delivery latency samples with irregular timestamps, compute for each sample the median of all latencies whose timestamps fall in the trailing window (ts - window, ts]. The window is defined in seconds, not in number of samples.

A Python data structures and time series question for Meta ads infrastructure engineers, where a rolling median is a robust signal for delivery regressions. The interviewer checks time-based eviction, maintaining order statistics efficiently, and correct median for even counts.

What the Interviewer Expects
  • Evict samples older than the window boundary
  • Maintain values in sorted order for median lookup
  • Handle even and odd window sizes
  • Handle duplicate timestamps and values
  • State complexity and the two-heap alternative
Key Topics to Cover
sliding window
rolling median
bisect
two heaps
Python
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you get O(log n) per update?
  • How would you make this a rolling p95?
  • How would you handle out-of-order timestamps?
  • How would you do this in pandas?
Sharpen Your Skills on Codemia

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

Practice SQL Problems
Sample Answer
Clarify the Data

Input: a list of (ts_seconds, value) sorted by ts, possibly with equal timestamps. Window is (ts - window, ts]: a sample exactly window seconds old is out. Output one median per input sample....

Approach

Keep a deque of samples in arrival order for eviction and a sorted list of the same values for the median. For each new sample, append to the deque, insert into the sorted list with bisect.insort, t...


Submit Your Answer
Markdown supported