We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Search Suggestions System

Number: 1397

Difficulty: Medium

Paid? No

Companies: DoorDash, Amazon, Squarepoint Capital, Google, Anduril, TikTok, Citadel, Salesforce, UBS, Microsoft, Oracle, Meesho, Deliveroo, Coursera, Snap, Apple, Meta, PayPal, J.P. Morgan, Two Sigma, Wix


Problem Description

Given an array of strings called products and a string called searchWord, design a system that suggests at most three product names after each character of searchWord is typed. The suggested products must share a common prefix with searchWord. If there are more than three matching products, return the three lexicographically smallest ones. The final output is a list of lists representing the suggestions after each character of searchWord is typed.


Key Insights

  • Sorting the products lexicographically simplifies finding candidates.
  • Using binary search helps efficiently locate the starting index for each prefix.
  • Limiting suggestions to at most three candidates for every prefix ensures optimal performance in both time and space.
  • Alternative methods include Trie-based search which can be efficient for prefix queries, but binary search after sorting is straightforward given the problem constraints.

Space and Time Complexity

Time Complexity: O(n log n + m log n) where n is the number of products and m is the length of searchWord. Sorting takes O(n log n) and for each of the m prefixes, binary search takes O(log n). Space Complexity: O(m + k) where m is the output size (list of lists for each prefix) and k is the additional space used by the sorting algorithm (which can be O(n) in worst-case depending on implementation).


Solution

The approach starts by sorting the input products lexicographically. For each prefix of searchWord, use binary search to quickly find the first product that could match the current prefix. Then, iterate through at most three products from that position and, if they share the prefix, add them to the suggestions for that stage. This process is repeated for every prefix in searchWord. This method leverages efficient sorting and binary search to meet the problem's requirements while ensuring the suggestions are lexicographically minimal.


Code Solutions

# Python solution for Search Suggestions System

import bisect

def suggestedProducts(products, searchWord):
    # Sort products lexicographically
    products.sort()
    result = []
    prefix = ""
    
    # For each character in searchWord, update the prefix
    for char in searchWord:
        prefix += char
        # Find the start index of matching products using binary search
        start = bisect.bisect_left(products, prefix)
        suggestions = []
        # Check next three products starting from 'start'
        for i in range(start, min(start + 3, len(products))):
            if products[i].startswith(prefix):
                suggestions.append(products[i])
            else:
                break
        result.append(suggestions)
    return result

# Example usage:
# products = ["mobile", "mouse", "moneypot", "monitor", "mousepad"]
# searchWord = "mouse"
# print(suggestedProducts(products, searchWord))
← Back to All Questions