Search Suggestions System - Problem

You are given an array of strings products and a string searchWord. Design a system that suggests at most three product names from products after each character of searchWord is typed.

Suggested products should have common prefix with searchWord. If there are more than three products with a common prefix, return the three lexicographically minimum products.

Return a list of lists of the suggested products after each character of searchWord is typed.

Input & Output

Example 1 — 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: Typing 'm': products starting with 'm' are ["mobile","moneypot","monitor","mouse","mousepad"], first 3 lexicographically are ["mobile","moneypot","monitor"]. Typing 'mo': same products match 'mo' prefix, so same result ["mobile","moneypot","monitor"]. Typing 'mou': only ["mouse","mousepad"] match, so return ["mouse","mousepad"]. For 'mous' and 'mouse': same result ["mouse","mousepad"].
Example 2 — No Matches
$ Input: products = ["havana"], searchWord = "havana"
Output: [["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
💡 Note: Each character of 'havana' matches the product 'havana', so we return ['havana'] for each prefix.
Example 3 — Empty Results
$ Input: products = ["bags","baggage","banner","box","cloths"], searchWord = "bags"
Output: [["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]
💡 Note: Typing 'b': ['baggage','bags','banner']. 'ba': ['baggage','bags','banner']. 'bag': ['baggage','bags']. 'bags': ['bags'].

Constraints

  • 1 ≤ products.length ≤ 1000
  • 1 ≤ products[i].length ≤ 3000
  • 1 ≤ searchWord.length ≤ 1000
  • products[i] and searchWord consist of lowercase English letters only

Visualization

Tap to expand
Search Suggestions System INPUT products[] (sorted): "mobile" "moneypot" "monitor" "mouse" "mousepad" searchWord: m o u s e Two Pointers: left = 0 right = 4 Valid range narrows as prefix grows longer ALGORITHM STEPS 1 Sort Products Lexicographic order 2 Init Two Pointers left=0, right=n-1 3 For Each Char Shrink invalid range 4 Collect Top 3 From valid range Prefix "m": [mobile,moneypot,monitor] Prefix "mo": [mouse,mousepad] (only 2 match) Prefix "mou","mous","mouse": [mouse,mousepad] (same) Range shrinks: [0,4]-->[3,4] FINAL RESULT Output (5 suggestion lists): "m" --> ["mobile","moneypot","monitor"] "mo" --> ["mouse","mousepad"] "mou" --> ["mouse","mousepad"] "mous" --> ["mouse","mousepad"] "mouse" --> ["mouse","mousepad"] OK - 5 Lists Generated Time: O(n log n + m*n) Space: O(log n) for sorting Key Insight: Sorting products enables efficient range-based search. Two pointers track the valid prefix range, shrinking it character by character. Products outside the prefix are eliminated permanently, so we never revisit them. First 3 items in the valid range are always lexicographically smallest. TutorialsPoint - Search Suggestions System | Two Pointers - Optimized Range Tracking
Asked in
Amazon 85 Microsoft 45 Google 30 Apple 25
127.0K Views
High Frequency
~15 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