Problem
Design an in-memory key–value store that supports basic operations and can report the average operation load over a recent time window.
Functional requirements
-
KV operations
-
PUT(key, value)
stores/overwrites a value.
-
GET(key)
retrieves the value (or
null
/not found).
-
Load/throughput metric query
-
Provide a function that can answer, for a given time window (e.g., the
last 10 minutes
):
-
Average GETs per second
during that window
-
Average PUTs per second
during that window
-
Example query: “In the past 10 minutes, what is the average GET QPS and PUT QPS?”
Clarifications to resolve with the interviewer (state assumptions if not provided)
-
Is the window always “last X” (sliding window) vs. “between start/end timestamps”? Assume
sliding window: last X
.
-
Maximum window size / retention (e.g., up to 1 year). Assume queries can be up to
1 year
.
-
Exactness vs approximation. Assume
exact counts
are preferred if feasible.
-
Concurrency expectations. Assume
multi-threaded
callers.
Non-functional goals
-
KV operations should be low latency.
-
Metric queries should be efficient for common windows (minutes/hours) and not require scanning raw logs.
-
Memory usage should be bounded given a maximum retention.