Autocomplete Business Names Ranked by Search Popularity
Last updated: November 29, 2026
Quick Overview
Build a structure that records searches or clicks for business names and, given a typed prefix, returns the k most popular matching names (ties alphabetical) in time proportional to the prefix length.
Yelp
November 29, 20260
0
0 solved
Build a structure that records searches or clicks for business names and, given a typed prefix, returns the k most popular matching names (ties alphabetical) in time proportional to the prefix length.
Typing "jo" into the Yelp search bar should instantly suggest Joe's Pizza before an obscure match. The interviewer is looking for a trie, and for the insight that caching the top k at each node makes queries O(prefix) instead of walking the whole subtree.
What the Interviewer Expects
- Choose a trie keyed by lowercased characters
- Cache the top k names at each node to make suggest independent of subtree size
- Update every node on the path when a count changes
- Explain why truncating to k is safe when counts only increase
- Discuss memory cost and alternatives such as sorted arrays with binary search
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Counts can now decrease (spam clicks removed). What breaks and how do you fix it?
- How would you personalize suggestions by the user's city?
- How would you support typo tolerance, like jolibee for Jollibee?
- How would you shard this across servers for tens of millions of names?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
record(name, delta) adds popularity (delta at least 1). suggest(prefix) returns up to k names whose lowercase form starts with the prefix, sorted by popularity descending, then name. Matching is c...
High-Level Design
A trie node per character, each holding a small list top of the best k names in its subtree. On record, walk the name's path (including the root for the empty prefix) and at each node insert the n...