How would you design a typeahead/autocomplete system?
655 views
75 of 106
1
The problem
Question presented to candidate:
"As a user types into our search box, we want to show the five most popular completions for what they have typed so far, updated on every keystroke. We have about 100 million daily users. How would you design that?"
What a strong answer should cover:
A capacity estimate that starts from keystrokes, not searches (requests per second after debouncing), and the size of the suggestion data
The core structure: a trie (prefix tree) where every node stores its precomputed top-k completions, so a lookup is a walk of length equal to the prefix, not a search of the subtree
The data pipeline: query logs aggregated with time decay, filtered, built into tries offline, swapped in atomically; plus a fast path for trending queries
Serving: in-memory replicas, sharding by prefix and its hot-spot problem, caching of short prefixes at the edge and in the browser
Client behaviour (debounce, cancel stale requests, cache results) and extensions: personalization, typo tolerance, filtering offensive suggestions
Clarifying questions expected:
How fresh must suggestions be: is a daily rebuild acceptable, or must trending queries appear within minutes?
Ranking by global popularity only, or personalized, or by location and language?
Do we need typo tolerance, or prefix matching only?
Code / implementation expected: Optional โ a trie with top-k cached per node and a comparison with searching the subtree; the example does both and shows a rebuild picking up a trending query.
autocomplete
trie
2
Try it yourself
Sketch your approach before you read the solution. That is what the interview tests.
Stuck? Get a hint.
A short nudge toward the approach. It will not give the answer away.
3
Solution
Target Audience: Engineers preparing for a system design round; you know what a server and a database are, and data structures like tries are explained from scratch.
Difficulty: Hard
Verified with: Node 22.22, Python 3.12, Go 1.24 and Java 21 (all four print identical output). The capacity figures are worked arithmetic from stated assumptions, not measurements of a real service.
How to read this doc: Every idea is explained in plain language first, then tagged with ๐ Interview term: so you also learn the exact words an interviewer expects. The capacity numbers and the trie lookups come from the example program; the Verified section quotes its full output.
1. Why This Even Matters โ A Story First
Think of a librarian who has worked at the same desk for twenty years. You start to say "I am looking for a book about ca..." and before you finish she says "cars,
The solution is hidden so you can try first
14 min read ยท 16 sections
4
Read the code
Typeahead trie with top-k cached in every node vs searching the subtree on each keystroke