The Design Search Autocomplete System coding problem is a sophisticated task where you design a real-time search suggestion engine. As a user types a sentence character by character, the system should return the top 3 historical sentences that start with the current prefix. Suggestions are ranked by frequency, with lexicographical order as a tie-breaker. When the user finishes a sentence (indicated by a '#'), the system records it or updates its frequency.
This is a high-signal "Hard" problem used by Google, Meta, and Amazon. It tests your ability to handle data streams and manage state across multiple function calls. It evaluations your mastery of the Trie interview pattern and your ability to perform top-K filtering using Priority Queues or sorting. It’s a perfect simulation of a real-world feature found in search engines and messaging apps.
The core pattern is a Trie (Prefix Tree). Each node in the trie represents a character and can store a map of sentences that pass through it along with their frequencies.
History: "i love you" (5 times), "island" (3 times), "ironman" (2 times).
To optimize retrieval, you can store a Map<String, Integer> of frequencies at each Trie node. While this uses more space, it allows you to get suggestions in where is the prefix length, rather than using DFS.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Design Add and Search Words Data Structure | Medium | Solve | |
| Design In-Memory File System | Hard | Solve | |
| Find Median from Data Stream | Hard | Solve | |
| Stream of Characters | Hard | Solve | |
| Implement Magic Dictionary | Medium | Solve |