Search Suggestions System - Problem
Build a Smart Search System
You're tasked with creating an auto-complete search system similar to what you see on Amazon or Google. Given an array of
Requirements:
• Return at most 3 product suggestions after each character is typed
• Products must have a common prefix with the current search input
• If more than 3 products match, return the 3 lexicographically smallest ones
• Return a list of lists representing suggestions after each character
Example: If products = ["mobile", "mouse", "moneypot", "monitor"] and searchWord = "mo", then:
• After typing 'm': ["mobile", "moneypot", "monitor"]
• After typing 'mo': ["mobile", "moneypot", "monitor"]
You're tasked with creating an auto-complete search system similar to what you see on Amazon or Google. Given an array of
products and a searchWord, your system should provide real-time suggestions as the user types each character.Requirements:
• Return at most 3 product suggestions after each character is typed
• Products must have a common prefix with the current search input
• If more than 3 products match, return the 3 lexicographically smallest ones
• Return a list of lists representing suggestions after each character
Example: If products = ["mobile", "mouse", "moneypot", "monitor"] and searchWord = "mo", then:
• After typing 'm': ["mobile", "moneypot", "monitor"]
• After typing 'mo': ["mobile", "moneypot", "monitor"]
Input & Output
example_1.py — Basic Case
$
Input:
products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
›
Output:
[["mobile","moneypot","monitor"],["mobile","moneypot","monitor"],["mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"]]
💡 Note:
After typing 'm': mobile, moneypot, monitor (lexicographically first 3). After 'mo': same 3. After 'mou': mouse, mousepad. After 'mous': mouse, mousepad. After 'mouse': mouse, mousepad.
example_2.py — Single Character
$
Input:
products = ["havana"], searchWord = "havana"
›
Output:
[["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
💡 Note:
Only one product exists, so it's suggested for every prefix of the search word.
example_3.py — No Matches
$
Input:
products = ["bags","baggage","banner","box","cloths"], searchWord = "bags"
›
Output:
[["baggage","bags","banner"],["baggage","bags"],["baggage","bags"],["bags"]]
💡 Note:
For 'b': baggage, bags, banner. For 'ba': baggage, bags. For 'bag': baggage, bags. For 'bags': only bags matches exactly.
Visualization
Tap to expand
Understanding the Visualization
1
Sort Products
Organize all products alphabetically for efficient searching
2
Type Character
User types a new character, extending the search prefix
3
Binary Search
Quickly find the range of products starting with current prefix
4
Return Top 3
Extract up to 3 lexicographically smallest matches
Key Takeaway
🎯 Key Insight: Pre-sorting enables binary search to find prefix ranges in O(log n) time, making the system responsive even with thousands of products
Time & Space Complexity
Time Complexity
O(n log n + m log n)
n log n for initial sorting, m characters × log n for binary searches
⚡ Linearithmic
Space Complexity
O(1)
Only using constant extra space for binary search
✓ Linear Space
Constraints
- 1 ≤ products.length ≤ 1000
- 1 ≤ products[i].length ≤ 3000
- 1 ≤ searchWord.length ≤ 1000
- products[i] and searchWord consist of lowercase English letters only
- All strings in products are unique
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code