Question bank

Design a system to suggest up to three product names from an array of strings based on a given searchWord. After each character is typed in searchWord, return a list of suggestions that share a common prefix with it. If more than three products match, return the three lexicographically smallest options. Provide the output as a list of lists corresponding to each character typed in searchWord

February 3, 20252 min read
MediumCodingAlgorithm DesignData StructuresProblem-SolvingSoftware EngineerProduct Manager
Design a system to suggest up to three product names from an array of strings based on a given searchWord. After each character is typed in searchWord, return a list of suggestions that share a common prefix with it. If more than three products match, return the three lexicographically smallest options. Provide the output as a list of lists corresponding to each character typed in searchWord

Approach Understanding Input and Output : Identify the input as an array of product names and a search word. The goal is to find names that match the prefix of the search word at each character input. Building Suggestions : For each character typed, filter…

Approach

  1. Understanding Input and Output: Identify the input as an array of product names and a search word. The goal is to find names that match the prefix of the search word at each character input.
  2. Building Suggestions: For each character typed, filter the product names based on the current prefix formed by the search word.
  3. Sorting and Limiting Results: Sort the filtered results lexicographically and limit the output to three suggestions.

Key Points

  • Prefix Matching: Ensure the suggestions start with the characters typed in the search word.
  • Lexicographical Order: Always sort the results to return the smallest names first.
  • Dynamic Response: Provide an updated list of suggestions at each character input.

Standard Response

def suggestedProducts(products, searchWord):
 products.sort() # Sort products lexicographically
 result = []
 prefix = ""
 
 for char in searchWord:
 prefix += char # Update prefix with the new character
 # Filter products that start with the current prefix
 suggestions = [product for product in products if product.startswith(prefix)]
 # Add the top three suggestions to the result
 result.append(suggestions[:3])
 
 return result

Tips & Variations

  • Common Mistakes to Avoid:
  • Failing to sort the product list initially can result in incorrect suggestions.
  • Not updating the prefix correctly with each character input will lead to inaccurate matches.
  • Alternative Ways to Answer:
  • Use a Trie data structure for more efficient prefix searching if handling a large dataset.
  • Role-Specific Variations:
  • Technical Roles: Emphasize the efficiency of your algorithm, perhaps discussing time complexity.
  • Creative Roles: Focus on the user experience and how intuitive the search feature is.

Follow-Up Questions

  • How would you modify your solution if the product list is extremely large?
  • Can you explain how you would implement this using a Trie?
  • What edge cases did you consider while developing your solution?
VA

Verve AI Editorial Team

Question Bank