System Design · Architecture under constraints
How would you design search autocomplete?
Short answer
Serve top suggestions for a prefix within a tight latency budget using a trie or precomputed prefix index, shard it, cache hot prefixes, and update rankings asynchronously.
Assume hundreds of millions of queries per day, a p99 suggestion latency under 100 milliseconds, prefix queries of one to twenty characters, and suggestions that should reflect popular and fresh queries. The read path must be fast and highly available; ranking can lag slightly.
Data structures
- Trie: walks the prefix character by character and returns the top-k completions stored at each node
- Prefix hash map: precompute the top-k list for every prefix, which is fast but memory-heavy
- Search index with edge n-grams: flexible ranking but usually slower than a purpose-built prefix index
- Sharding by prefix range or a hash of the prefix, with replication for availability
- Cache the top prefixes at the edge because the distribution is highly skewed
A trie can be compressed into a radix tree to save memory. Store only the top-k suggestions at each node, where k is small, rather than every descendant. That bounds the response size and makes a lookup proportional to the prefix length plus k, not to the vocabulary size.
Updates come from query logs and content changes. Rebuild or update the index asynchronously, because a write-heavy real-time trie is expensive to keep consistent. Rank by a blend of frequency, recency and personalization. Apply a decay so stale viral queries do not dominate forever, and filter unsafe or policy-violating terms before they enter the index.
Personalization changes the cache key. A shared cache can serve the global top-k, while a per-user layer reranks a small candidate set. If personalization is expensive, keep it off the critical path and return the global result first.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1How do you keep the trie small enough for memory?
- 2How do you handle new or trending queries?
- 3What would you cache and with what key?
- 4How do you filter offensive suggestions?
Say it in the interview
Autocomplete is a latency problem: for a prefix, return a small ranked set of completions. I would use a compressed trie or precomputed prefix index that stores only the top-k at each node, shard it, and cache hot prefixes at the edge. Updates and ranking run asynchronously from query logs with frequency and recency decay. Personalization reranks a small candidate set off the critical path so the shared cache still works.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live System Design mock interview and get scored feedback on depth, clarity and confidence.