Lexicographic Search Suggestions
Updated
Problem statement
Given unique lowercase product names and a lowercase searchWord, return suggestions after each character of searchWord is typed.
For every non-empty prefix, return at most three products that start with that prefix. Choose the first three matching names in alphabetical order, or all matching names if there are fewer than three.
If no product matches a prefix, return an empty list for that prefix and every longer prefix.
Function
suggestedProducts(products: String[], searchWord: String) → List<List<String>>Examples
Example 1
products = ["mobile","mouse","moneypot","monitor","mousepad"]searchWord = "mouse"return = [["mobile","moneypot","monitor"],["mobile","moneypot","monitor"],["mouse","mousepad"],["mouse","mousepad"],["mouse","mousepad"]]The first two prefixes have at least three matches. From prefix mou onward, only mouse and mousepad remain.
Example 2
products = ["havana"]searchWord = "havana"return = [["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]The only product matches every typed prefix.
Example 3
products = ["bags","baggage","banner","box","cloths"]searchWord = "bags"return = [["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]Each longer prefix narrows the lexicographically sorted matching range.
Constraints
1 <= products.length <= 100000.- Product names are unique lowercase English strings.
1 <= products[i].length <= 100.1 <= searchWord.length <= 1000, and it contains lowercase English letters.- The total number of product characters is at most
500000.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Find the Words for Each PrefixDocuSign · PHONE SCREENDocuSign interview questions
- Keyboard Autocomplete SuggestionsLyft · ONSITE INTERVIEWLyft interview questions
- Word Break IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Longest Word Containing Another InputMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Word Search IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Find Unmet Users With Shared InterestsPalantir · ONSITE INTERVIEWPalantir interview questions
- Search a Sorted Array of WordsExpedia · ONSITE INTERVIEWExpedia interview questions
- Product Record SearchWells Fargo · OAWells Fargo interview questions
- Find All Word Occurrences in TextSentry · PHONE SCREENSentry interview questions