Design Search Autocomplete
🔎 What is search autocomplete? It is the list of suggestions that drops down under a search box while you type. After every keystroke, the system looks at what is in the box so far and shows the few past searches that start with it, most popular first. When you finish a search, it is remembered, so popular searches keep getting more popular.
Autocomplete looks like a pure data structure question, and the core of it is one: a trie. But interviewers use it as a design problem because the hard parts are design choices. Which object remembers what the user has typed so far? Where does the ranking work happen: on every keystroke, or once when a sentence is saved? And what changes when many people type at once? A good answer gets the trie right quickly and spends the rest of the time on those questions.
Requirements
"Design a search autocomplete system. As the user types, show the three most popular past searches that start with what they have typed."
Before reaching for a trie, find out what "popular", "start with" and "typed" mean here. Each answer below settles one rule the code has to follow.
Clarifying Questions
You: Where does popularity come from?
Interviewer: The system starts with a history: a list of sentences and how many times each one was searched. After that, every sentence a user finishes counts as one more search for it.
You: How do we know a sentence is finished? Do we get the whole query at once?
Interviewer: No, we get one character per call, the way a browser sends keystrokes. A special character, #, means the user pressed Enter. Return the suggestions after every normal character, and nothing for #.
You: What if two sentences have the same count?
Interviewer: Break ties alphabetically, in plain ASCII order. So a space comes before any letter.
You: Does the half-typed sentence itself count? If I have typed "i lo", is "i lo" a candidate?
Interviewer: No. A sentence only enters the history when it is finished with #.
You: Does "start with" mean the beginning of the sentence, or the beginning of any word in it?
Interviewer: The beginning of the whole sentence. "island" is a match for "is", but "i love you" is not a match for "lo".
You: How big is the history, and how often do people type?
Interviewer: Up to a few hundred sentences of up to 100 characters to start, and thousands of keystrokes. Suggestions are requested on every keystroke; new sentences only arrive when someone presses Enter.
You: One search box, or many users at once?
Interviewer: One box for now. We can talk about many users later.
The answer to the "how often" question is the one that shapes the design. Reads (a keystroke) happen roughly ten times as often as writes (a finished sentence), so it is worth doing extra work on each write if it makes each read cheap. Keep that in mind; it decides the Great solution below.
💡 Interview tip: Always ask how reads and writes compare. "Reads are ten times more common" is the single fact that tells you where to spend work. Here it justifies caching the answer at every prefix.
Final Requirements
- The system starts with a history of distinct sentences, each with a count of how many times it was searched.
- Input arrives one character at a time: lowercase letters, spaces, and
#. - After each normal character, return up to 3 sentences from the history that start with everything typed since the last
#. - Rank by count, highest first. Equal counts are ordered by ASCII order of the sentence.
- When
#arrives, the typed sentence is saved: its count goes up by one, or it joins the history with count 1. The box is cleared and nothing is returned. - The sentence being typed does not count until it is finished.
- Suggestions are requested much more often than sentences are saved, so each keystroke must be fast.
Out of scope
- Typo tolerance ("ilsand" suggesting "island") and matching words in the middle of a sentence.
- Personal suggestions per user, and many users typing at the same time (we come back to both in Extensibility).
- Removing or blocking sentences (also in Extensibility).
- Storing the history across machines. That is the system design version of this problem; see /system-design/typeahead.