Search Autocomplete
Typeahead suggestions from a prefix tree
Design the object behind a search box that shows the most searched queries starting with what the user has typed so far, and learns from every search.
Overview
As a user types into a search box, the box shows a short list of complete queries that start with the letters typed so far, most popular first. Every submitted search is recorded, so tomorrow's suggestions reflect today's searches. The low level design version asks for one class with two operations: suggest(prefix) returns the top k queries for a prefix, and record_search(query) counts a search.
The problem tests three things. First, whether you pick a data structure whose lookup cost depends on the prefix, not on how many queries are stored: a trie (prefix tree), where each node is one character and the path from the root spells a prefix. Second, whether you see that walking every word under a short prefix like 'a' on every keystroke is too slow, and store each node's top k answers on the node instead. Third, whether you keep the stored lists correct as counts change, and safe when searches are recorded while other users read.
A strong answer names the trie in the first minute, says 'precompute the top k per node', explains why that list stays correct when counts only go up, and says what breaks when they can go down (recency decay).
Scoping the problem
The design rests on seven questions. Each bullet gives the question, why it matters, and the answer this chapter uses.
- How many suggestions? This sets k, the length of each node's list. The demo uses 3; real boxes show 5 to 10.
- Ranked by what? Ranking by count alone keeps the lists simple, while recency or personalization changes the update rule. Rank by search count, with ties broken alphabetically.
- What about case and spaces? 'Car ' and 'car' must be one query. Trim, collapse spaces and lowercase.
- When does a search count: on every keystroke, or on submit? Keystrokes are not searches, so count on submit only.
- Must a new search show up at once? This is the choice between an online update and a nightly rebuild. The class updates at once; a rebuild is a follow-up.
- Many users at the same time? Then reads and writes need a lock or snapshots. Yes.
- Can queries be deleted, for abuse or legal removal? Removal lowers a count, which breaks the simple update rule. Treat it as a follow-up.
Functional requirements
- suggest(prefix) returns up to k stored queries that start with the prefix, most searched first
- Ties in count break alphabetically so results are stable
- record_search(query) counts a submitted search and updates the suggestions at once
- Input is trimmed, inner spaces collapsed and lowercased, for both stored queries and prefixes
- An empty prefix or a prefix with no match returns an empty list
- The ranking rule is a replaceable policy object
Non-functional requirements
- suggest is O(L + k) for a prefix of length L, independent of how many queries are stored
- record_search is O(L × k log k)
- Safe with many readers and writers at once: no lost counts, no list that disagrees with the counts
- Deterministic: same searches, same suggestions
Interview questions
- Why not just run a LIKE prefix query against the database?
- Recent searches should count more than old ones. What changes?
- How do you remove a query, for example an offensive one?
- Where is the lock, and what would you do if reads vastly outnumber writes?
- How does this scale past one machine?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio