Rank Yahoo Search Autocomplete Suggestions With a Frequency Trie
Last updated: November 29, 2026
Quick Overview
Build an autocomplete structure over historical search queries and their counts. Support adding queries and returning the top k completions for a prefix, ordered by count and then alphabetically.
Yahoo
November 29, 20260
0
0 solved
Build an autocomplete structure over historical search queries and their counts. Support adding queries and returning the top k completions for a prefix, ordered by count and then alphabetically.
Search assist on Yahoo's homepage and search box needs fast prefix lookups ranked by popularity. Interviewers look for a clean trie, correct ranking with tie-breaks, and a discussion of precomputing top-k per node for latency.
What the Interviewer Expects
- Design trie nodes with children and a terminal count
- Walk to the prefix node and collect completions in its subtree
- Keep only k candidates using a bounded heap
- Handle case normalization and missing prefixes
- Discuss caching top-k at each node to make queries O(prefix length)
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
- How would you make suggest O(len(prefix)) at query time?
- How do you update rankings as live query counts change every minute?
- How would you filter offensive or policy-violating completions?
- How would you personalize suggestions per user or location?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Autocomplete(query_counts) builds from a dict. add(query, times) increments a query count. suggest(prefix, k) returns up to k full queries starting with prefix, sorted by count descending, the...
High-Level Design
A character trie where each node stores count > 0 if a query ends there. suggest walks down the prefix, then does a DFS over the subtree, keeping a min-heap of size k keyed on `(count, reversed st...