Full answer
Requirements
Functional:
- As the user types "pyth", return up to 10 suggestions such as "python tutorial", "python interview questions".
- Rank by popularity, with recent trends reflected; filter offensive or blocked terms.
- Support several languages.
Non-functional:
- Very low latency: the full round trip should feel instant, so the server target is a few milliseconds and p99 under about 50 ms.
- Highly available; slightly stale suggestions are fine.
- Read-heavy; updates can lag by minutes or hours.
Capacity estimates
Assumptions: 100M DAU, 5 searches per user per day, 6 suggestion requests per search after client debouncing (about 150 ms), peak factor 2, 100M distinct popular queries, 500M distinct prefixes kept, 100 bytes per prefix entry (prefix plus 10 query IDs).
- Requests: 100M x 5 x 6 = 3B per day = about 34,700 per second; about 69,000 per second at peak.
- Data: 500M prefixes x 100 B = 50 GB, plus the query strings (100M x about 25 bytes = 2.5 GB). Fits in memory across a small sharded cluster, or even on one large machine per replica.
- Caching: a large share of requests are for 1 to 3 character prefixes, which are few in number and cache perfectly at the CDN and in the browser.
API
GET /v1/suggest?q=pyth&lang=en&limit=10
200 Cache-Control: public, max-age=300
{"q":"pyth","suggestions":["python tutorial","python interview questions", ...]}
Data model
- Serving: map
prefix -> [query_id x 10]plusquery_id -> text. A trie with top-k stored at each node is the textbook form; a flat hash map keyed by prefix is simpler to shard and serves the same lookups. - Offline:
query_counts(query, window, count)aggregated from logs.
High-level design
flowchart LR
U[Browser / app] --> CDN[CDN cache]
CDN --> SS[Suggest service]
SS --> SH[(Prefix shards in memory)]
SL[Search logs] --> K[Kafka]
K --> AGG[Batch aggregation: Spark]
K --> TR[Streaming trends: Flink]
AGG --> BUILD[Index builder]
TR --> BUILD
BUILD --> SNAP[(Snapshot in object storage)]
SNAP --> SHBuilding the index
- Search logs go into Kafka.
- A batch job counts queries over a window (for example the last 30 days) with time decay so old popularity fades, removes blocked terms and very rare queries.
- For each query, add it as a candidate to every prefix of it (up to a limit like 20 characters), keep the top 10 per prefix by score.
- Write a versioned snapshot; serving nodes load it and switch atomically. Rollback means loading the previous snapshot.
- A streaming job detects sudden spikes ("exam results 2026") and pushes small overrides that merge into results until the next batch build.
Serving and scaling
Shard by hash of the prefix so load spreads evenly; sharding by first letter would make "s" and "a" shards hot. Replicate each shard for throughput and availability. Each lookup is one in-memory read, so a modest number of replicas handles about 69,000 requests per second, and the CDN absorbs much of it.
Personalisation (a user's own recent searches) is merged on the client or by a thin layer on top, rather than precomputed per user.
Bottlenecks and trade-offs
- The bottleneck is not storage but peak read rate on short prefixes and the size of the offline build; the CDN and browser cache take the first, incremental builds the second.
- Precomputed top-k per prefix gives constant-time reads but needs large offline builds and lags behind reality; the streaming layer reduces lag at extra complexity.
- Storing top-k at every prefix uses more memory than computing it at query time from a trie, but query-time traversal would be too slow for popular short prefixes.
- Aggressive caching makes blocked-term removal slower to take effect; keep short TTLs and a blocklist check at the service so a removal applies within minutes.
By level
Fresher: Explain a trie, how prefixes map to top suggestions, and why the client waits a little between keystrokes before sending. A fresher can describe a trie and top suggestions per prefix.
Mid: Estimate traffic and memory, design the offline pipeline and snapshot loading, shard by prefix hash and cache at the edge. Expect an estimate of requests and memory with stated assumptions.
Senior: Add streaming trends, multilingual and personalised layers, safe rollout and rollback of snapshots, abuse and manipulation of popularity, and cost of rebuilding at scale. Expect safe rollout and rollback of index snapshots.
Key points
- Precompute top-k per prefix offline from query logs
- Serve from sharded in-memory maps or tries in milliseconds
- About 3B requests per day, roughly 35k per second average
- Debounce on the client and cache short prefixes at the CDN
- Streaming layer adds trending terms between batch builds
Common mistakes
- Computing suggestions by scanning a database with LIKE 'prefix%' per keystroke
- Sharding by first letter and creating hot shards
- Sending a request for every keystroke without debouncing
- Forgetting filtering of offensive or blocked terms
Follow-ups
- How would you add personalised suggestions?
- How do you handle typos in the prefix?
- How would you remove a harmful suggestion within minutes?
Tips
- Ask about freshness needs; they decide batch versus streaming
- Mention client debounce and CDN caching early
- Explain why top-k is stored per prefix
Evaluation rubric
- Requirements emphasise latency and freshness
- Request and memory estimates with assumptions
- Offline build plus online serving design
- Sharding, caching and freshness trade-offs