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, 2025Updated September 16, 20262 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

Related reads

Explore More Question Bank Entries

How would you implement an algorithm to sort a stack data structure?
January 4, 2025Medium

How would you implement an algorithm to sort a stack data structure?

Approach To effectively answer the interview question on implementing an algorithm to sort a stack data structure, follow this structured framework: Understand the Problem : Clearly articulate what is required when sorting a stack. Choose the Right Algorithm…

Read answer guide
How would you implement an algorithm to determine the number of ways to split a given string?
January 11, 2025Medium

How would you implement an algorithm to determine the number of ways to split a given string?

Approach When faced with the interview question, "How would you implement an algorithm to determine the number of ways to split a given string?" it's essential to break down your thought process into structured steps. Here’s a comprehensive framework to…

Read answer guide
How would you implement an algorithm to calculate the sum of all left leaves in a binary tree?
January 13, 2025Medium

How would you implement an algorithm to calculate the sum of all left leaves in a binary tree?

Approach To effectively answer the question, “How would you implement an algorithm to calculate the sum of all left leaves in a binary tree?”, follow this structured framework: Understand the Problem : Clarify the definition of left leaves in the context of…

Read answer guide
How can you implement an algorithm to calculate the number of ways to tile a floor?
January 20, 2025Medium

How can you implement an algorithm to calculate the number of ways to tile a floor?

Approach To effectively answer the question of implementing an algorithm to calculate the number of ways to tile a floor, follow this structured framework: Understand the Problem : Clearly define what is meant by "tiling a floor" and the constraints involved…

Read answer guide
How would you implement an algorithm to calculate the number of unique binary search trees that can be formed with 'n' distinct nodes?
January 20, 2025Hard

How would you implement an algorithm to calculate the number of unique binary search trees that can be formed with 'n' distinct nodes?

Approach To effectively answer the question on implementing an algorithm to calculate the number of unique binary search trees (BSTs) that can be formed with 'n' distinct nodes, follow this structured framework: Understand the Problem : Define what a binary…

Read answer guide
How would you implement an algorithm to check if a string contains all unique characters without using additional data structures?
January 10, 2025Medium

How would you implement an algorithm to check if a string contains all unique characters without using additional data structures?

Approach To effectively answer the question about implementing an algorithm to check if a string has all unique characters without using additional data structures, follow this structured framework: Understand the Problem : Clarify what "unique characters"…

Read answer guide
How would you implement an algorithm to count the number of valid combinations of parentheses?
January 18, 2025Medium

How would you implement an algorithm to count the number of valid combinations of parentheses?

Approach To effectively answer the question "How would you implement an algorithm to count the number of valid combinations of parentheses?", follow this structured framework: Understand the Problem : Identify what constitutes a valid combination of…

Read answer guide
How would you implement a function to compress a string by replacing consecutive repeated characters with their counts?
January 18, 2025Medium

How would you implement a function to compress a string by replacing consecutive repeated characters with their counts?

Approach To effectively answer the question, "How would you implement a function to compress a string by replacing consecutive repeated characters with their counts?", follow this structured framework: Understand the Requirements : Break down what the…

Read answer guide
How do you implement the Bellman-Ford algorithm to find the shortest path in a graph?
January 2, 2025Hard

How do you implement the Bellman-Ford algorithm to find the shortest path in a graph?

Approach To effectively answer the question, "How do you implement the Bellman-Ford algorithm to find the shortest path in a graph?", follow this structured framework: Understanding the Bellman-Ford Algorithm : Begin with a brief explanation of the algorithm…

Read answer guide