Reported September 2026
Amazonbacktracking

Word Break II

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

Amazon reported this Word Break II question in September 2026, and the constraint that matters is s.length <= 20 paired with an output cap of 10^5 total characters. Those numbers tell you exactly what's expected. Brute-forcing every way to place spaces is 2^19 splits, which sounds scary but isn't the real problem. The real problem is wasting work on dead-end prefixes and recomputing the same suffixes. This is a string segmentation problem solved with backtracking plus memoization. If your head goes blank mid-assessment, StealthCoder runs invisibly as a safety net, but you should know the shape of this one before you start.

The problem

Given a string s and an array of unique dictionary words wordDict, insert spaces into s so that every resulting token is a dictionary word.
Return every valid sentence in lexicographic order. A dictionary word may be reused any number of times.

Function
wordBreak(s: String, wordDict: String[]) → String[]

Examples
Example 1
s = "catsanddog"
wordDict = ["cat","cats","and","sand","dog"]
return = ["cat sand dog","cats and dog"]
The string can be segmented as cat sand dog or cats and dog. The two sentences are returned in lexicographic order.
Example 2
s = "pineapplepenapple"
wordDict = ["apple","pen","applepen","pine","pineapple"]
return = ["pine apple pen apple","pine applepen apple","pineapple pen apple"]
All three sentences concatenate to pineapplepenapple, and dictionary words such as apple may be reused.
Example 3
s = "catsandog"
wordDict = ["cats","dog","sand","and","cat"]
return = []
No sequence of dictionary words concatenates to the entire string.

Constraints
1 <= s.length <= 20
1 <= wordDict.length <= 1000
1 <= wordDict[i].length <= 10
s and every wordDict[i] contain only lowercase English letters.
All strings in wordDict are unique.
The total length of all valid output sentences does not exceed 10^5.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to recurse on the start index. At index i, try every dictionary word that matches s starting at i, then recurse on i + len(word). Memoize the list of sentences for each start index so repeated suffixes aren't rebuilt. Put the words in a hash set and only check substrings up to length 10, since that's the max word length. The common pitfall is skipping memoization and timing out on strings like aaaa...a with a dictionary of a, aa, aaa. Another pitfall is forgetting the lexicographic order requirement. Sort the final list, or sort the dictionary first and build in order, though sorting at the end is safer because joined sentences can interleave. Return an empty list when no split reaches the end of the string. If you freeze on the memo structure during the live OA, StealthCoder can surface the working recursion so you can verify your approach instead of starting cold.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Word Break II cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as word break ii. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Word Break II FAQ

How hard is Word Break II really?+

It's a LeetCode Hard on paper, but with s.length <= 20 it's friendlier than it looks. If you can write a recursive function over the start index and add a memo map, you're basically done. The hard part is generating all sentences cleanly, not the algorithm.

What's the trick to avoid timing out?+

Memoize by start index. Store every sentence that can be built from that suffix. Without it, repeated suffixes get recomputed over and over. Also cap substring checks at length 10 and use a hash set for dictionary lookups so each check is cheap.

Do I need dynamic programming or is backtracking enough?+

Backtracking with memoization is the cleanest answer and counts as top-down DP. You can also run a bottom-up reachability check first to prune dead ends, but with a length limit of 20 it's optional. Plain memoized recursion passes comfortably.

How do I handle the lexicographic order requirement?+

Collect all sentences, then sort the result list before returning. Don't assume dictionary order gives you sorted output, because words of different lengths can interleave at the same position. One sort at the end is simple and correct.

How do I prepare for this in 48 hours?+

Write Word Break I first, then extend it to return the sentences. Practice the memo version from scratch twice. Test the no-solution case and the reuse case, like repeated words. Know how to join word lists with spaces cleanly so you don't leave trailing spaces.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it