Scenario
Design a service that tracks user search queries and returns the Top-K most searched words/queries.
Requirements
Functional
-
API to
record
a search query event (e.g., user searches "iphone 16").
-
API to fetch
Top-K
queries.
-
Support a time window variant:
-
(a) all-time Top-K
-
(b) Top-K over the last
N minutes/hours
(trending)
Non-functional
-
High write QPS (search events), low latency reads for Top-K.
-
Scalable horizontally.
-
Reasonable correctness (define whether exact or approximate is acceptable).
Clarifications to address
-
What is a “word” vs “query” (full string)?
-
Case-folding, normalization, stop words, punctuation.
-
Deduping (e.g., multiple searches by same user) or count every event.
-
Multi-region and eventual consistency expectations.