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 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
Search Suggestions SystemSearch: mo🔍Suggestions:📱 mobile💰 moneypot🖥️ monitorAll Products (Sorted):bagmobile ✓moneypot ✓monitor ✓mouse ✓notebook💡 Binary search finds the range of products starting with "mo" in O(log n) time⚡ Much faster than checking every product individually
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

n
2n
Linearithmic
Space Complexity
O(1)

Only using constant extra space for binary search

n
2n
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
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28 Apple 22
73.9K Views
High Frequency
~18 min Avg. Time
1.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