Scenario
You need to design an in-memory component that tracks how often each key appears in a stream of events.
The component must support these operations:
-
record(key)
: increment the count for
key
by 1 (insert
key
if not present).
-
topK(k)
: return the
k
keys with the highest counts so far (order among them does not matter).
Assume:
-
There can be up to
N
distinct keys.
-
Operations happen online as the stream arrives.
Task
Describe how you would implement this component under two different workload patterns:
-
Read-heavy
: there are many more
topK(k)
calls than
record(key)
calls.
-
Write-heavy
: there are many more
record(key)
calls than
topK(k)
calls.
For each case, specify:
-
The main data structures you would use.
-
Time complexity of
record
and
topK
.
-
Space complexity.
-
Why your design is appropriate for that workload (the trade-offs you’re making).