Compute last-5-minute QPS in memory
DatabricksSoftware Engineer · Technical ScreenLast updated
This question evaluates design of in-memory data structures and sliding-window time-series aggregation for real-time metrics, testing understanding of space/time trade-offs, streaming algorithms, and handling monotonic timestamps.
Problem
You are building a lightweight in-memory component that tracks the query load (QPS) of a service.
Design a data structure with two operations:
record(timestamp)— called once per incoming request.getQPS(timestamp)— returns the average QPS over the last 5 minutes ending attimestamp.
Define the QPS at time t as:
[ \text{QPS}(t) = \frac{#{\text{requests with } time \in (t-300,\ t] }}{300} ]
Requirements / Constraints
- Timestamps are in seconds.
- Calls to
recordandgetQPSmay be interleaved. - Assume timestamps are non-decreasing across calls.
- Aim for efficient per-call time complexity.
Follow-up questions
- How would you implement this using a **sliding window**?
- How can you **reduce memory usage** while keeping performance good (e.g., avoid storing every request timestamp)?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.