Grokking the Coding Interview: Patterns for Coding Questions
Vote

0% completed

Solution: Search Suggestions System

Problem Statement

Given a list of distinct strings products and a string searchWord.

Determine a set of product suggestions after each character of the search word is typed. Every time a character is typed, return a list containing up to three product names from the products list that have the same prefix as the typed string.

If there are more than 3 matching products, return 3 lexicographically smallest products. These product names should be returned in lexicographical (alphabetical) order.

Examples

  1. Example 1:

.....

.....

.....

Like the course? Get enrolled and start learning!
M

Murtuza Chhatriwala

· 2 years ago

Wouldn't BFS be needed instead of DFS in order to comply with - "If there are more than 3 matching products, return 3 lexicographically smallest products. These product names should be returned in lexicographical (alphabetical) order."

Show 2 replies
Em Eff

Em Eff

· a year ago

Specifically, submitting my solution fails following test case is fails, even when copying the solution into the challenge verbatim:

Your Input ["mobile","mouse","moneypot","monitor","mousepad"] "mouse"
Output [["mobile","mouse","mousepad"],["mobile","mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"]]
Expected [["mobile","moneypot","monitor"],["mobile","moneypot","monitor"],["mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"]]

This is actually very confusing because running my solution (instead of submitting) gives a different expected output for the same input mentioned above:

Expected [["mobile","mouse","mousepad"],["mobile","mouse","mousepad"],["mouse","mousepad"],["mou