Case study V.5Intermediate

Design Search Autocomplete

Every keystroke is a request, which makes this one of the busiest endpoints a company runs. The trick is to do the expensive ranking offline and make serving a plain memory lookup.

The short answer

Requirements. Return the top 5 completions for a prefix, ranked by popularity, with trending searches within about an hour and blocked terms never shown; answer in under 100 ms end to end, about 20 ms of it on the server.

Requests
23,000 per second2B keystrokes a day, peak about 50,000
Prefix index
about 40 GB1B prefixes × 5 IDs × 8 B, so shard it
Server budget
about 20 msof 100 ms end to end

The design. Ranking happens offline. Search logs feed a batch job that counts the last few weeks and a streaming job that counts the last hour; a builder merges the scores into a trie that stores the top 5 completions at every node, and serving nodes swap it in. Serving walks down one node per typed character and returns the stored list. Debounce in the client, cache short prefixes at the CDN, and shard by prefix range balanced by traffic.

Interview prompt

Design the suggestions that appear under a search box as the user types.

Requirements and estimates

Functional requirements

  • Return the top 5 completions for a prefix, ranked by popularity.
  • Trending searches appear within about an hour.
  • Offensive and blocked terms never appear.

Non-functional requirements

  • Under 100 ms end to end, so the server budget is about 20 ms.
  • Highly available; if suggestions fail, search still works.
  • Freshness of minutes to hours is fine.

Estimates

Requests     10M DAU × 10 searches × 20 keystrokes = 2B per day, ≈ 23,000/s
Peak         ≈ 2×                                   ≈ 50,000/s
Prefix index ≈ 1B prefixes × 5 IDs × 8 B         ≈ 40 GB plus keys, so shard it

API and data model

API

GET /v1/suggest?q=tw&lang=en
→ 200 Cache-Control: max-age=3600
  { "suggestions": ["twitter", "twitch",
    "twilight", "twins", "tweetdeck"] }

What each trie node stores

prefix  top 5 completions, precomputed
t       twitter, twitch, tiktok, target, translate
tw      twitter, twitch, twilight, twins, tweetdeck
twi     twitter, twitch, twilight, twins, twine

Storing the top five at every node is what makes serving fast: a lookup walks down as many nodes as the prefix has characters and returns the stored list, with no need to search the subtree below.

Whiteboard

What you sketch in the first five minutes: the trie that makes a lookup cheap, then the pipeline that builds it away from the request.

1. The top 5 at every node

Say: never search the subtree while the user waits: store the answer at the node.

ttwtwitwitter, twitch, tiktok,target, translate1. twitter2. twitch3. twilight4. twins5. tweetdecktwitter, twitch, twilight, twins, twineuser typestw|2 steps down,return the listno search below
  1. Walk one node per character: t, then tw.
  2. Each node already holds its top 5 completions.
  3. Return that list, with no search below the node.

Next question: a harmful term starts trending. Serving filters on a block list right away, and the next build leaves it out.

2. Build offline, swap in

Say: the expensive ranking happens away from the request.

searchlogsbatch: weeksstream: 1 hourbuild trieobjectstoragetrending terms within the hourload, then swapserving nodeserving nodeserving noderequests read the old trie until the swapand never wait for a build
  1. Search logs flow into a batch count of weeks and a stream count of the last hour.
  2. A builder merges the scores and writes a new trie to object storage.
  3. Serving nodes load it and swap it in atomically.

Next question: how fresh is it? Trending terms appear within the hour, from the stream count.

Where suggestions live

ApproachServingUpdatingFits when
Trie in memory, top 5 per node Common pickWalk the prefix, return the listRebuilt offline and swapped inThe classic interview answer
Prefix → top 5 in a key value storeOne GET per keystrokeBatch write every prefixSimple to shard and cache, more storage
Search engine completion suggesterIn memory automaton with fuzzy matchingNear real time indexingYou already run Elasticsearch or OpenSearch and need typo tolerance

The data pipeline

Search logs flow into a stream. A batch job counts queries over the last few weeks with older counts weighted less; a streaming job counts the last hour to catch trending terms. The scores are merged, a builder produces a new trie, writes it to object storage, and serving nodes load it and swap it in atomically. Reads never wait for writes.

Cutting the load

  • Debounce in the client: send only after a short pause in typing.
  • Cache responses in the browser, and at the CDN for short prefixes, which are the hottest and identical for everyone.
  • Shard by prefix range using a shard map balanced by traffic, because far more queries start with “s” than with “x”.

What the interviewer probes next

A harmful term starts trending. How do you remove it right now?

Filter results against a blocklist at serving time, before returning them. The next rebuild removes the term from the trie itself.

How would you personalize suggestions?

Blend the global top results with the user’s own recent searches, kept on the device or in a small per user store, and merge them in the client or at the edge.

How do you handle typos?

Fuzzy matching by edit distance. It is much more expensive, so run it only when the exact prefix returns few results; completion suggesters in search engines support it directly.

What about other languages?

Build one trie per language or region, normalize Unicode, and for languages typed through an input method, such as Chinese pinyin, index the typed form as well as the final characters.

Building blocks used here

Want to run this as a mock interview? See interview prep.

© 2026 Arman Ajabkhani · armantik.com · Privacy