Search Auto-complete
Solve a medium-level problem of designing an auto-complete feature using tries.
We'll cover the following...
Problem statement
Auto-complete is a feature—similar to one provided by search engines—that displays query suggestions in real time based on what search string a user is typing in the input field.
We're provided with a list of keywords and a query string. Design a system that provides the top three suggestions from the keyword list to the user every time the user types a character of the query string.
The suggested words should be a prefix of the query string typed so far. If there are more than three valid suggestions, return the three lexicographically smallest ones.
Example
Sample input
words: ["honey", "hammer", "hostile", "hotel", "hill", "hotdog", "hotter"]queryWord: "hot"
Sample output
query typed so far = "h",suggestions: ["hammer", "hill", "honey"]query typed so far = "ho",suggestions: ["honey", "hostile", "hotdog"]query typed so far = "hot",suggestions: ["hotdog", "hotel", "hotter"]
Explanation
For the query "h", words sorted in alphabetical order are ["hammer", "hill", "honey", "hostile", "hotdog", "hotel", "hotter"], out of which the top three are "hammer", "hill" and "honey".
For the query "ho", words sorted in alphabetical order are ["honey", "hostile", "hotdog", "hotel", "hotter"], out of which the top three are "honey", "hostile" and "hotdog".
For the query "hot", words sorted in alphabetical order are ["hotdog", "hotel", "hotter"], out of which the top three are "hotdog", "hotel", and "hotter".
Try it yourself
Try to solve the problem yourself before reading the solution.
Intuition
Binary search-based approach
Since the problem asks us to return the results in sorted order, we sort the words as a first step. This sorting operation allows us to perform a binary search for a given prefix value in the words list. Once a prefix is found in the list of words, we need to add the next three words to the output suggestion list.
To summarize the steps performed in the algorithm, we sort the words list and iterate each character of the query, adding it to the prefix to search for, and then run a binary search for the prefix in the sorted word list. Then, starting from the current binary search start index, we add the next three strings to the output and continue doing this till the prefix remains the same.
The time complexity of the above solution is