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
Coding & Algorithms
Software Engineer
Uber
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
backtracking
memoization
word break
string segmentation
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
  • 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 Problems
Sample 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 ...


Submit Your Answer
Markdown supported