Word Break
How the interviewer asks it
“Given a string and a dictionary of words, decide whether the string can be split into a sequence of dictionary words. Words may be reused.”
leetcode leet code
true
In the practice room your code reads the input from standard input, and Run checks it against tests like this one. Try it now in your browser, in Python or JavaScript.
Typed drills are free and unlimited: the AI scores your answer against the outline below.
What the interviewer is looking for
- You spot overlapping subproblems in the naive recursion and add memoisation.
- A clean DP: can[i] is true if some word ends at i and can[i − len(word)] is true.
- You limit the inner loop by the longest dictionary word.
- You can sketch the follow-up that returns every split.
How to structure your answer
- 01
Clarify
Can the dictionary be large? Is the empty string valid? Are words case-sensitive?
- 02
Recursion
Try every dictionary prefix and recurse on the rest; explain why it is exponential without memoisation.
- 03
DP over prefixes
can[0] = true. For each end index i, check the words (or substrings up to the longest word) that end at i.
- 04
Test
"icecream" with [ice, cream] is true; "catsandog" with [cats, dog, sand, and, cat] is false.
- 05
Complexity
O(n × L) substring checks for maximum word length L, with a hash set dictionary.
Best answers from candidates
No candidate answers yet. Type yours, and if it scores 6 or more you can show it here without your name.
Answers are shared by candidates, without names, after a privacy check. Add yours
Common mistakes
- Greedy matching of the longest word first, which fails on "catsand" style inputs.
- Forgetting that substring creation and hashing cost O(L) each.
- Off-by-one errors in the DP index.
Follow-up questions to expect
- Return every possible sentence, not just true or false.
- The dictionary has a million words. Would a trie help?
Now answer it out loud
Max will ask you this exact question, listen to your answer, push on the weak spots with follow-ups like the ones above, and score you when you finish.
Practice with MaxFree: a 5-minute voice round and unlimited typed drills, with scored feedback. No card, and your camera is never recorded.
More coding interview questions
Preparing for this round? Read how to prepare for a coding interview: a four-week DSA plan.