Quick revision
The short answer
The API is GET /suggest?q=<prefix>&locale=..&limit=10, and clients debounce by about 100 to 150 ms, cancel stale requests and reuse results for prefixes already typed. The data is a precomputed list of top suggestions for each normalised prefix, built from a trie of candidate queries with the top-k stored at each node, so a lookup costs only the length of the prefix. It is served from memory on replicated servers, very short prefixes are cached at the CDN edge, and the same normalisation and a blocklist apply at build time and query time.
What to remember
In this answer, we designed autocomplete around a tight latency budget. The API is GET /suggest with a prefix, locale and limit, and the client debounces, cancels stale requests and reuses earlier results. The data is a trie of candidate queries with the top-k stored at each node, so a lookup costs only the prefix length, served entirely from memory on replicated servers and sharded by prefix range only if it outgrows one machine. Short prefixes are cached at the CDN edge, and the same normalisation and a blocklist apply at build time and query time.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The API is GET /suggest?q=<prefix>&locale=..&limit=10, and clients debounce by about 100 to 150 ms, cancel stale requests and reuse results for prefixes already typed. The data is a precomputed list of top suggestions for each normalised prefix, built from a trie of candidate queries with the top-k stored at each node, so a lookup costs only the length of the prefix. It is served from memory on replicated servers, very short prefixes are cached at the CDN edge, and the same normalisation and a blocklist apply at build time and query time.
Why this question is asked
Autocomplete is a small feature with a hard deadline. If suggestions arrive after the person has typed the next letter, they are useless, so the whole design is about answering in a few milliseconds while traffic arrives on nearly every keystroke. Interviewers ask it to see whether we precompute instead of searching at request time, and whether we use the client and the edge to cut load. In this answer, we will set the latency and traffic budget, define the API and client behaviour, design the trie with top-k per node, serve it from memory, cache short prefixes at the edge, and handle normalisation and safety.
1The Budget: About 100 ms, Most of It Already Spent
A person types a character roughly every 150 to 300 ms, and suggestions should appear before the next one, within about 100 ms end to end. The network round trip from a phone may take 40 to 60 ms of that, and the browser needs time to render. So the server has a few tens of milliseconds, and the lookup itself should take well under 5 ms so that queueing and the network have room.
At 20,000 requests per second, any design that searches a database at request time, such as a LIKE 'pres%' query sorted by popularity, would be both too slow and too expensive. The answer must already exist when the request arrives; the server's job is only to find it.
2The API and What the Client Does
The API is one read endpoint. The client sends the prefix, the locale and a limit, and gets back a ranked list.
The client does a lot of the work. It debounces, meaning it waits about 100 to 150 ms after a keystroke before sending, so a fast typist sends one request for several letters. It cancels an in-flight request when a new letter arrives, because an answer for an older prefix is no longer wanted. And it keeps the results of prefixes already typed, so backspacing needs no request. Together these can cut requests per search by half or more.
3The Data Model: A Trie With Top-k at Each Node
The candidates are popular past queries, perhaps 10 million per locale, each with a score from its frequency. A trie is a tree in which each node stands for a prefix and each edge adds one character, so all queries starting with 'pres' sit below the node for 'pres'.
Finding the top 10 under a node by walking its whole subtree would be slow for short prefixes, which have millions of descendants. So at build time we store at each node its precomputed top 10 suggestions with their scores. A lookup then walks down the trie one character at a time, which costs time proportional to the prefix length, and reads the list stored at the final node. No sorting happens at request time.
| Prefix node | Stored top suggestions (score) |
|---|---|
| p | pizza near me (98), paypal (95), pinterest (90), ... |
| pr | prime video (80), presidents day (61), printer (55), ... |
| pre | presidents day (61), presbyopia (22), pregnancy test (20), ... |
| pres | presidents day (61), presbyopia (22), press release template (9), ... |
4Serving From Memory
Let us check that this fits in RAM. Storing the top-k lists for prefixes of up to about 20 characters over 10 million candidates gives at most a few hundred million nodes in the worst case, far fewer in practice because queries share prefixes. If each node's list holds 10 suggestion ids of 4 bytes, plus the node itself, a locale's structure is a few GB. Many implementations flatten it into a sorted array or a hash map from prefix to list, which is compact and simple to load.
Each suggest server loads the whole structure into memory and answers from it, so a lookup is a few microseconds of work. We replicate the structure across many servers behind a load balancer; at, say, 5,000 requests per second per server, 20,000 requests per second needs four servers plus spares in each region. We shard by prefix range, for example a to m on one group and n to z on another, only if the structure outgrows one machine's memory.
- Clientdebounce, cancel stale requests, local cache
- CDN edgecaches 1 to 3 character prefixes for a few minutes
- Load balancerspreads misses across suggest servers
- Suggest serverswhole prefix structure in memory, replicated
- Offline builderbuilds the structure from query logs, publishes snapshots
5 layers, from the top down. Step through them one at a time.
5Caching Short Prefixes at the Edge
Very short prefixes, one to three characters, are both the most frequent requests, since every search starts with them, and the same for everyone in a locale. There are only a few thousand of them per locale for three letters of a common alphabet. That makes them ideal for the CDN: with a cache lifetime of a few minutes, the edge answers them close to the user, saving both latency and origin load. Longer prefixes are more varied and mostly go to the origin, which is fine because the servers are fast.
6Normalisation and Safety
A prefix typed as 'Pres', 'pres' or 'pres ' should give the same suggestions. So we normalise case, accents where the locale allows, and repeated or trailing spaces, and we must apply exactly the same normalisation at build time, when we compute the prefix keys, and at query time, when we look them up. If the two differ, lookups miss for no visible reason.
Suggestions are also content we show unprompted, so before publishing a new structure we filter it through a blocklist of offensive, dangerous or legally sensitive phrases, and through rules for sensitive categories. Filtering at build time costs nothing at request time.
7Follow-up questions
How do you rank suggestions beyond raw popularity?
The score at build time can combine frequency with recency, click-through on past suggestions, and locale. Light signals such as the user's region can be applied at request time by choosing a region-specific structure, without per-user computation.
How do you handle typos?
A simple approach adds common misspellings as alternative keys pointing to the right suggestions during the build. Fuzzy matching at request time is possible but costs more and is usually reserved for prefixes with no good exact match.
How are updates published?
The builder produces a new structure, servers load it in the background and swap it in atomically, so requests always see one complete version. The next question in this series goes deeper into keeping it fresh.
8Conclusion
In this answer, we designed autocomplete around a tight latency budget. The API is GET /suggest with a prefix, locale and limit, and the client debounces, cancels stale requests and reuses earlier results. The data is a trie of candidate queries with the top-k stored at each node, so a lookup costs only the prefix length, served entirely from memory on replicated servers and sharded by prefix range only if it outgrows one machine. Short prefixes are cached at the CDN edge, and the same normalisation and a blocklist apply at build time and query time.
Open with
The server has only a few tens of milliseconds, so I would precompute everything. The API is GET /suggest with the prefix, locale and a limit of 10, returning ranked suggestions.
Then, if they want more
Clients debounce by about 100 to 150 ms, cancel stale requests when a new key arrives, and reuse results for prefixes already typed.
For data, I build a trie of candidate queries from search logs and store at each node the precomputed top 10 with scores, so a lookup just walks the prefix and reads one list: the cost is proportional to the prefix length, with no sorting at request time.
For millions of candidate queries this prefix to top-k structure is a few GB, so it fits in RAM. I would load it on every suggest server, replicate across servers and regions, and only shard by prefix range if it outgrows one machine. At 20,000 requests per second that is a handful of servers.
Prefixes of one to three characters are the most frequent and the same for everyone in a locale, so I cache them at the CDN with short TTLs. Finally, I normalise case, accents and spacing identically at build time and query time, and filter suggestions through a blocklist before publishing a new version.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design the API, data model and serving path of an autocomplete service that returns the...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/750a8326eb/