Split a Run-Together Search Query Into Catalog Keywords
Last updated: November 29, 2026
Quick Overview
Customers sometimes type search queries without spaces, like "wirelessearbudscase". Given a set of catalog keywords, return one way to split the query into keywords, or None if no split exists.
Amazon
November 29, 20260
0
0 solved
Customers sometimes type search queries without spaces, like "wirelessearbudscase". Given a set of catalog keywords, return one way to split the query into keywords, or None if no split exists.
Amazon search must normalize messy queries before retrieval, and word segmentation is a classic step. The interviewer wants the word break DP with a back-pointer to reconstruct a split, plus the max keyword length optimization.
What the Interviewer Expects
- Define ok[i] as whether query[:i] can be split
- Try only end windows up to the longest keyword
- Store back pointers to rebuild one split
- Return None when ok[n] is false
- O(n * L) substring checks
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return the split with the fewest keywords?
- How would you rank splits by keyword popularity?
- Use a trie: how does it change the inner loop?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: string query and list keywords. Output: a list of keywords whose concatenation equals the query, or None. Any valid split is acceptable....
High-Level Design
Recursion that tries every prefix is exponential on inputs like aaaa...b. DP fixes it:
ok[i]is true ifquery[:i]can be fully split.ok[0] = True.ok[i]is true if some j hasok[j]...