Generate Prefix-Based Product Suggestions
Company: Citadel
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: Generate up to three lexicographically smallest product suggestions for every prefix of a search word, with efficient handling once a prefix has no matches.
Read the full Citadel Software Engineer interview experience this question came from
Constraints
- 1 <= len(products) <= 50000.
- Every product has length from 1 through 100, and product names are distinct.
- 1 <= len(search_word) <= 100.
- All product names and the search word contain only lowercase English letters.
- Return at most three suggestions for every nonempty search prefix.
Examples
Input: (['mobile', 'mouse', 'moneypot', 'monitor', 'mousepad'], 'mouse')
Expected Output: [['mobile', 'moneypot', 'monitor'], ['mobile', 'moneypot', 'monitor'], ['mouse', 'mousepad'], ['mouse', 'mousepad'], ['mouse', 'mousepad']]
Explanation: This is the source example; each successive prefix narrows the sorted matching range.
Input: (['apple', 'banana'], 'cat')
Expected Output: [[], [], []]
Explanation: No product matches the first prefix, so all longer prefix results are empty.
Hints
- Sorting makes every prefix's matches a contiguous range.
- Binary-search the range boundaries, then take only its first three entries.