Scenario
You implemented a function to compute “top K” (e.g., most frequent items). Now design a backend service that provides similar functionality at scale.
Requirements
Design a service that continuously computes and serves the top K most frequent items (e.g., hashtags, products, search queries).
Functional requirements
-
Ingest a high-volume stream of events: each event includes at least
(item_id, timestamp)
.
-
Support an API to query
GET /topk?k=K&window=...
returning the top-K items by frequency.
-
Support a time window, at minimum:
-
Either
all-time
counts, or
-
A
sliding window
(e.g., last 5 minutes / last 1 hour). State your choice/assumptions.
Non-functional requirements
-
Low latency queries (e.g., p95 < 100 ms) at high QPS.
-
High write throughput ingestion.
-
Horizontal scalability (add machines to scale).
-
Fault tolerance and reasonable correctness guarantees (clearly state consistency model).
Clarifications to address
-
What are the expected event rate, number of distinct items, and query QPS?
-
Do you need exact results or are approximate results acceptable?