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
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

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
1D dynamic programming
word break
back pointers
hash set
trie
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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 if query[:i] can be fully split.
  • ok[0] = True.
  • ok[i] is true if some j has ok[j]...

Submit Your Answer
Markdown supported