Implement an in-memory traffic counter.
You need to support two operations for each logical key, such as an endpoint, customer, or service name:
-
record(key, timestamp)
: a request for
key
happened at
timestamp
.
-
get_qps(key, now)
: return the average queries per second for
key
over the last 5 minutes, using the time window
[now - 299, now]
.
Requirements:
-
All data must be stored in memory.
-
Timestamps are integer seconds.
-
Multiple requests may arrive with the same
key
and the same
timestamp
.
-
Start with a working solution for a 5-minute sliding window.
Follow-up discussion:
-
How would you reduce memory usage?
-
How would you extend the design to support a 24-hour window?
-
How would you efficiently handle many requests that share the same timestamp?
Assume calls arrive in non-decreasing timestamp order.