Design search autocomplete (type-ahead suggestions)
depth · 45 min · senior/staff
Setting
You own the search box on a high-traffic site. As a user types, they expect a dropdown of relevant, popular completions to appear after every keystroke, ranked by how often people actually search for them.
The ask
Design the system that, given a partial query, returns the top-K completions ranked by popularity, updated as real query traffic changes over time.
Constraints & scale
- ~1B searches/day feeding the popularity signal; the autocomplete endpoint itself serves far more requests (one per keystroke)
- p99 latency budget < 50 ms per keystroke — this is on the critical path of every character typed
- Suggestions should reflect recent trends (a breaking-news term should surface within minutes) without a full index rebuild
- Must support prefix matching efficiently at scale, and degrade gracefully (no suggestions rather than a slow or broken search box) if the ranking service is unhealthy
- Personalization/localization is a bonus — a correct, fast, un-personalized version is the core bar
What a strong answer covers
Requirements → estimates (QPS, index size) → data structure choice (trie / other) and how popularity counts feed it → high-level diagram (query-log ingestion, ranking/aggregation, serving index) → 1–2 deep dives → trade-offs. Likely probes: how the serving index stays both fast and fresh, the trie/alternative trade-off at this scale, and the graceful-degradation path.
A 45-minute plan
- 0-5 min · Requirements
- 5-10 min · Estimates/API
- 10-25 min · High-level
- 25-40 min · Deep dive
- 40-45 min · Wrap
What to cover, in order
- Requirements
- Estimates (QPS, index size)
- Data structure choice (trie / other) and how popularity counts feed it
- High-level diagram (query-log ingestion, ranking/aggregation, serving index)
- 1–2 deep dives
- Trade-offs
Where the follow-up questions go
Pick one of these for your deep dive before the interviewer picks for you.
- How the serving index stays both fast and fresh
- The trie/alternative trade-off at this scale
- The graceful-degradation path
Key trade-offs
Decisions a strong answer weighs, and what pushes each one either way.
- Trie in memory or an index. An in-memory trie with top results stored per node gives lookups proportional to the prefix length, and uses memory and needs a rebuild process. A search index or sorted store is easier to operate and slower per request. Memory cost depends on how many distinct queries you keep.
- How many queries to keep. Keeping every query ever typed includes typos and one-off strings that will never be suggested. Pruning by minimum count or recency shrinks the structure a lot and drops legitimate rare queries.
- Freshness against stability. Updating counts continuously surfaces trends within minutes and makes suggestions jump around and costs writes. Rebuilding on a schedule is stable and slow to react. A common split is a large, stable base plus a small, fast-moving layer of recent trends merged at query time.
- Where to cache. Caching responses at the edge or in the browser removes most requests, since popular prefixes repeat constantly. Short prefixes are the best candidates. Caching longer, rarer prefixes costs more than it saves.
- Client behaviour. Sending a request on every keystroke is the most responsive and multiplies load. Debouncing and cancelling stale requests reduce it with little visible delay. Client-side matching on short prefixes can answer without a request.
Common mistakes
What goes wrong most often on this problem, and why it hurts.
- Computing matches at request time. Scanning or sorting every query that starts with a prefix will not hold at keystroke volume. Precompute the best results per prefix.
- Updating the serving structure from the write path. Logging each search into the live trie couples your read latency to write volume. Aggregate separately and publish updates.
- Ignoring unsafe or unwanted suggestions. Popular does not mean appropriate. You need a filter list that applies before results are served, and a way to add to it quickly.
- Treating all prefixes alike. One-character prefixes are enormous in volume and low in value, and can be fully precomputed and cached. Long prefixes are rare.
- Failing loudly. A broken suggestion service should result in no dropdown, not a frozen or erroring search box.
Graded on
Requirements & scoping · Estimation · API & data model · High-level architecture · Deep dive · Scalability & performance · Reliability & consistency · Trade-offs · Operability · Communication & drive