You are building a monitoring component for a key–value (KV) store. Each request contributes 1 unit of load at its request time.
Design a data structure that supports the following operations efficiently:
record(t)
: a request arrives at integer timestamp
t
(in seconds). Timestamps are non-decreasing across calls.
avgLoad(t)
: return the
average load per second
over the last
5 minutes
ending at time
t
, i.e., over the interval
(t - 300, t]
.
0
.
avgLoad(t)
should return a floating-point value:
If requests were recorded at times 1, 2, 300, 301:
avgLoad(301) = 4 / 300
avgLoad(600) = 0 / 300 = 0
avgLoad(t)
should be faster than scanning all historical events.