Design a rolling-window event tracker for latency count, average, min, max and percentile
Company: Plaid
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a class that ingests a stream of timestamped events (such as API calls with latency values) and provides real-time statistics over a configurable rolling time window.
The rolling window is defined by a `windowSize` parameter. An event with timestamp `t` is considered within the window for a query at time `queryTime` if `t > queryTime - windowSize`, so the window is the half-open interval `(queryTime - windowSize, queryTime]`. Events are guaranteed to arrive with non-decreasing timestamps.
The system should support recording events and querying the count, average latency, minimum latency, maximum latency, and p-th percentile latency (using the nearest-rank method) over the events currently in the rolling window.
Implement the following methods:
- `EventStatistics(windowSize: int)`: Create the tracker with the given window length.
- `recordEvent(timestamp: int, latency: int)`: Record an event with the given timestamp and latency. Timestamps are guaranteed to be non-decreasing across calls.
- `getCount(timestamp: int) -> int`: Return the number of events within the rolling window ending at the given timestamp.
- `getAverage(timestamp: int) -> float`: Return the average latency of events within the rolling window, rounded to 2 decimal places. Returns 0.0 if no events exist in the window.
- `getMin(timestamp: int) -> int`: Return the minimum latency among events in the rolling window. Returns -1 if no events exist in the window.
- `getMax(timestamp: int) -> int`: Return the maximum latency among events in the rolling window. Returns -1 if no events exist in the window.
- `getPercentile(timestamp: int, percentile: int) -> int`: Return the p-th percentile latency using the nearest-rank method over events in the rolling window. Returns -1 if no events exist in the window.
Example with `windowSize = 10`:
```text
EventStatistics(10)
recordEvent(1, 100)
recordEvent(2, 300)
recordEvent(5, 200)
recordEvent(8, 400)
getCount(8) -> 4
getAverage(8) -> 250.0
getPercentile(8, 50) -> 200
getPercentile(8, 90) -> 400
recordEvent(12, 160)
getCount(12) -> 3
getAverage(12) -> 253.33
getMin(12) -> 160
getMax(12) -> 400
getPercentile(12, 50) -> 200
getCount(15) -> 2
getMin(30) -> -1
getAverage(30) -> 0.0
```
At time 8 all four events are inside `(-2, 8]` and the sorted latencies are `[100, 200, 300, 400]`, so the 50th percentile is the 2nd smallest and the 90th is the 4th. At time 12 the window is `(2, 12]`, so the events at times 1 and 2 have expired and the live latencies are `[160, 200, 400]`. At time 15 the event at time 5 expires too, and by time 30 the window is empty.
```hint Expiring old events
Timestamps never go backward, so the events that leave the window are always the oldest ones still stored. Pick a container that makes removing from that end cheap.
```
```hint Order statistics
Min, max and percentile are all positions in the sorted order of the live latencies. Work out what each insert and each expiry really costs in the structure you choose, and compute the percentile rank without floating-point error.
```
### Constraints & Assumptions
- `1 <= windowSize <= 10^6`
- `1 <= timestamp <= 10^9`
- `1 <= latency <= 10^6`
- `0 <= percentile <= 100`
- Timestamps for `recordEvent` calls are non-decreasing.
- Query timestamps are `>=` the latest recorded event timestamp.
- At most `10^5` total operations.
### Clarifying Questions to Ask
- Can a query timestamp be earlier than the timestamp of a previous query, or do all calls move forward in time?
- What should `getPercentile` return for `percentile = 0`, where the nearest-rank formula gives rank 0?
- Should the average round half-up, or is the language default (half-to-even in Python) acceptable?
- Are exact percentiles required, or would an approximate answer be acceptable if the window grows very large?
### What a Strong Answer Covers
- How expired events are located and removed, including the exact boundary at `queryTime - windowSize`
- The structure that answers min, max and percentile, and the real cost of its inserts and removals, including element shifting
- How the average stays current without rescanning the window on every query
- The nearest-rank calculation, covering `percentile = 0`, `percentile = 100` and floating-point error in the rank
- Empty-window results and the rounding rule for the average
### Follow-up Questions
- How would you guarantee logarithmic time per operation when all `10^5` events fall inside one window?
- How would the design change if events can arrive out of order by up to a few seconds?
- How would you answer queries at arbitrary past timestamps, which pruning on read cannot serve?
- At millions of events per second across many endpoints, how would you report p99 latency with bounded memory?
Overview: Design a class that records timestamped latency events and reports the count, average, minimum, maximum and nearest-rank percentile over a rolling time window. It tests sliding-window expiry, the choice of a structure for order statistics, and careful handling of the window boundary, rounding and empty windows.