Design Search Autocomplete System - Problem

Imagine building the autocomplete system for a major search engine like Google! Your task is to create an intelligent system that suggests the most relevant search queries as users type.

You'll implement an AutocompleteSystem class that:

  • Learns from historical search data - both the sentences and how many times each was searched
  • Provides real-time suggestions as users type character by character
  • Returns the top 3 hottest matching sentences based on search frequency
  • Handles tie-breaking using ASCII order (lexicographical)
  • Completes searches when users type the special character '#'

Example: If historical data shows "i love you" was searched 5 times and "island" was searched 3 times, then typing "i" should return both suggestions, with "i love you" ranked higher due to its popularity.

The system must be responsive and intelligent, just like the autocomplete you use every day!

Input & Output

example_1.py — Basic Usage
$ Input: sentences = ["i love you", "island", "i love leetcode"], times = [5, 3, 2] Input sequence: 'i', ' ', 'a', '#'
Output: ['i love you', 'island', 'i love leetcode'] → ['i love you', 'i love leetcode'] → [] → []
💡 Note: Initially 'i' matches all three sentences, sorted by frequency. After space, only sentences with 'i ' prefix match. 'a' has no matches. '#' ends input and returns empty list.
example_2.py — New Sentence Addition
$ Input: sentences = ["i love you", "island"], times = [5, 3] Input sequence: 'i', '#', 'i', ' ', 'a', '#', 'i', ' ', 'a'
Output: ['i love you', 'island'] → [] → ['i love you', 'island'] → ['i love you'] → [] → ['i love you', 'i a']
💡 Note: After typing 'i#', the sentence 'i' is added with frequency 1. After 'i a#', the sentence 'i a' is added. Both new sentences appear in subsequent searches.
example_3.py — ASCII Ordering
$ Input: sentences = ["abc", "abd", "abe"], times = [2, 2, 2] Input sequence: 'a', 'b'
Output: ['abc', 'abd', 'abe'] → ['abc', 'abd', 'abe']
💡 Note: All sentences have the same frequency (2), so they are returned in ASCII (lexicographical) order: 'abc' < 'abd' < 'abe'.

Visualization

Tap to expand
🔍 Smart Search Autocomplete SystemHistorical Data"i love you" → 5 times"island" → 3 times"i am" → 2 timesTrie StructurePrefix TreeReal-time SuggestionsUser types: "i"Returns: Top 3 matches⚡ Instant responseHow It Works: Step by Step1. Build TrieInsert sentencesStore frequenciesPre-sort results2. NavigateFollow prefix pathO(m) complexityEfficient lookup3. Get ResultsAccess sorted listTop K selectionInstant retrieval4. LearnAdd new sentencesUpdate frequenciesContinuous improve⚡ Performance Metrics✅ Query Time: O(m + k log k)✅ Space: O(N × M × ALPHABET)🚀 Real-time: < 1ms response📈 Scalable: Millions of queries
Understanding the Visualization
1
Data Ingestion
Historical search data (sentences and frequencies) is processed and stored in an efficient Trie structure
2
Real-time Processing
Each character typed navigates deeper into the Trie, instantly accessing pre-computed suggestions
3
Smart Ranking
Results are ranked by popularity (search frequency) with alphabetical tie-breaking
4
Learning System
New searches (ending with '#') are added to the system, continuously improving suggestions
Key Takeaway
🎯 Key Insight: Using a Trie with pre-sorted sentence lists at each node transforms expensive search operations into instant lookups, making it perfect for real-time autocomplete systems that need to handle millions of queries efficiently.

Time & Space Complexity

Time Complexity
⏱️
O(m + k log k)

m is prefix length to navigate Trie, k log k for priority queue operations on matching sentences

n
2n
Linearithmic
Space Complexity
O(ALPHABET_SIZE × N × M)

Trie storage where N is number of sentences, M is average length, ALPHABET_SIZE is character set size

n
2n
Linear Space

Constraints

  • 1 ≤ sentences.length ≤ 100
  • 1 ≤ sentences[i].length ≤ 100
  • 1 ≤ times[i] ≤ 50
  • At most 5000 calls will be made to input
  • Each character in input will be a lowercase English letter, space ' ', or '#'
  • Sentences will only contain lowercase English letters and spaces
Asked in
Google 75 Amazon 50 Meta 40 Microsoft 35
127.5K Views
Very High Frequency
~35 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen