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.
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 at
timestamp
.
Define the QPS at time t as:
record
and
getQPS
may be interleaved.