Product Keyword Search
Updated
Problem statement
An online store has a list of products. Product i has the id productIds[i] and the name productNames[i]. A customer searches the store by typing a query made of one or more keywords.
A word is a run of characters with no spaces in it. In a product name and in the query, words are separated by one or more spaces, and there may also be spaces at the start or the end.
A product matches the query when every keyword in the query is equal to some word in the product name, ignoring letter case. The keywords can appear in the name in any order, and they do not need to be next to each other. A keyword must equal a whole word: milk matches Milk, but it does not match Milky. If a keyword appears more than once in the query, the name still needs it only once.
Return the ids of all matching products, in the same order as the products appear in the input. If no product matches, return an empty array.
Function
searchProducts(productIds: int[], productNames: String[], query: String) → int[]Examples
Example 1
productIds = [1,2,3,4,5]productNames = ["Amul milk","Nandini Chocolate Milk","Milk Chocolate","Chocolate Milk","Milky Way Chocolate Bar"]query = "Milk"return = [1,2,3,4]Products 1 to 4 each have the word Milk. Product 1 writes it as milk, and letter case is ignored. Product 5 has the word Milky, which is a different word, so it is left out.
Example 2
productIds = [1,2,3,4,5]productNames = ["Amul milk","Nandini Chocolate Milk","Milk Chocolate","Chocolate Milk","Milky Way Chocolate Bar"]query = "Chocolate Milk"return = [2,3,4]The query has two keywords, Chocolate and Milk. Products 2, 3 and 4 have both words, in either order. Product 1 has no Chocolate, and product 5 has Chocolate but no Milk.
Example 3
productIds = [10,20,30,40]productNames = ["Whole Wheat Bread","bread rolls","Breadsticks","Garlic BREAD Loaf"]query = " bread "return = [10,20,40]The spaces around bread only separate words, so the query has one keyword, bread. Products 10, 20 and 40 have that word in some letter case. Breadsticks is a longer word, so product 30 does not match.
Example 4
productIds = [7,3]productNames = ["Green Tea","Lemon Cake"]query = "Lemon Tea"return = []Product 7 has Tea but no Lemon. Product 3 has Lemon but no Tea. Neither product has both keywords, so the answer is an empty array.
Constraints
1 <= productIds.length <= 10^4productNames.length == productIds.length1 <= productIds[i] <= 10^9- All values in
productIdsare distinct. 1 <= productNames[i].length <= 1001 <= query.length <= 100productNames[i]andquerycontain only English letters, digits, and spaces, and each has at least one letter or digit.
Similar questions at other companies
- Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Single-Condition Product FilterCrowdstrike · PHONE SCREENCrowdstrike interview questions
- Convert an Entry List to a MapEPAM · PHONE SCREENEPAM interview questions
- Most Frequent NameAmazon · PHONE SCREEN
- Words with Repeated AnagramsThe Voleon Group · PHONE SCREEN
- Project JSON Fields into a Target FormatBooking.com · PHONE SCREEN
- Verify Words Sorted by a Custom AlphabetTrueFoundry · PHONE SCREEN
- Character Frequencies Across Nested String ListsSnowflake · PHONE SCREENSnowflake interview questions
- Neighboring Record IDsZipRecruiter · PHONE SCREENZipRecruiter interview questions