List All Ways to Split a Run-Together Street Address
Last updated: November 29, 2026
Quick Overview
Street names were pasted without spaces, for example `marketstreetsanfrancisco`. Given the string and a dictionary of known words, return every way to insert spaces so that each piece is a dictionary word.
Uber
November 29, 20260
0
0 solved
Street names were pasted without spaces, for example `marketstreetsanfrancisco`. Given the string and a dictionary of known words, return every way to insert spaces so that each piece is a dictionary word.
A hard Uber onsite question rooted in cleaning address data for geocoding. It tests backtracking with memoization over suffixes and an awareness that output can be exponential.
What the Interviewer Expects
- Recurses on suffix start index and tries every dictionary word prefix
- Memoizes results per start index to avoid recomputation
- Limits prefix lengths to the longest dictionary word
- Returns all sentences, not just a boolean
- Discusses exponential output size
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
- Return only whether a split exists, in O(n^2).
- Return the split with the fewest words.
- How would you rank splits using word frequencies from real addresses?
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
s, list of dictionary words. - Output: list of space-separated sentences, any order.
- Words can be reused....
High-Level Design
Define splits(i) as all sentences that segment s[i:]. For each end j up to i + max_len, if s[i:j] is a word, prepend it to every sentence in splits(j). The base case splits(len(s)) is a ...