Segment a Spaceless Spotlight Query Into the Fewest Dictionary Words

Last updated: November 29, 2026

Quick Overview

Users sometimes type queries without spaces, such as 'applemusicstudent'. Given a dictionary of known words, split the query into dictionary words using as few words as possible and return them, or return None if no segmentation exists.

Apple
Coding & Algorithms
Software Engineer
Apple
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Users sometimes type queries without spaces, such as 'applemusicstudent'. Given a dictionary of known words, split the query into dictionary words using as few words as possible and return them, or return None if no segmentation exists.

Spotlight and keyboard teams care about tokenizing messy input, and this question tests 1D dynamic programming with reconstruction. The interviewer looks for a DP over prefix positions, a bound by max word length, and a clean back-pointer walk.

What the Interviewer Expects
  • Define dp[i] as the fewest words for the first i characters
  • Only try word lengths up to the longest dictionary word
  • Store back-pointers to reconstruct the answer
  • Return None when dp[n] is unreachable
  • Compare with exponential backtracking
Key Topics to Cover
dynamic programming
string segmentation
back-pointers
hash sets
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 all segmentations with the minimum word count.
  • Rank segmentations by word frequency instead of count.
  • Words may have small typos; allow up to one edit per word.
  • The dictionary has millions of words; how does a trie help?
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 and iterable words. Output: a list of words that concatenate to s with minimum length, or None. Empty s returns an empty list. Ties may return any minimal segmentation; this ve...

High-Level Design

Let dp[i] be the fewest words covering s[:i], with dp[0] = 0. For each end i, try every start j with i - j <= maxlen; if s[j:i] is a word and dp[j] is reachable, then `dp[i] = min(dp[i...


Submit Your Answer
Markdown supported