Lesson 2 of 15
34. Design Search Autocomplete (Typeahead)

34.1 What do you ask first?

3 min read+10 XP

Step 1: Requirements (5 minutes)

LetterCandidate asksInterviewer answers
Scope"Suggestions are past searches, or book titles too? Personalised? Multiple languages? Spelling correction?""Popular past searches, plus new book titles. Not personalised for now. English only. Skip spelling correction."
Count"How many users and searches?""10 million daily active users, about 3 searches each a day."
Access pattern"Do we call on every keystroke?""Yes, after the client waits ~100 ms between keystrokes."
Latency"How fast must suggestions appear?""Faster than the user types: under 100 ms end to end."
Exactness"Must suggestions reflect searches from the last minute? Exact popularity?""Roughly right is fine. New launches should show up the same day."

Agreed requirements:

  • Functional (verbs): as the user types a prefix, return the top 5 suggestions; learn from searches over time; surface trending queries and new titles within hours; filter inappropriate suggestions.
  • Non-functional (adjectives): very fast (server time under ~20 ms, total under 100 ms), highly available (if suggestions fail, search still works), scalable for keystroke traffic, fresh within hours, safe (no offensive or manipulated suggestions).

Interviewer pushback:

  • "Why not just query the search index (Chapter 19) on every keystroke?" → A full search ranks documents and is far too slow and heavy to run per keystroke at this volume. Suggestions should be a precomputed lookup, built offline from what people search.
  • "What's 'top'?" → Ranked by a score: mostly how often a query is searched, blended with how fast it's rising (34.8).

Memory code: "Every keystroke is a request; answer from a table, not a search."