Reported September 2026
Wexmemoization

Word Break II

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

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

The mistake that sinks a first attempt at Word Break II is plain backtracking with no memo, and Wex candidates reported this one in September 2026. It looks friendly because s tops out at 16 characters. Then a string like aaaa with words a, aa, aaa blows up into a pile of repeated work, and the sorted-output requirement catches people who forget it. You need to return every valid sentence, in lexicographic order, with word reuse allowed. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and can hand you a working solution while you keep your cool.

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. Sentences must be sorted 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. The judged order is lexicographic.
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 <= 16.
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.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The pattern is recursion with memoization over the start index. Put the dictionary in a set. For each start position, try every end up to 10 characters ahead (the max word length), and if s[start:end] is a word, recurse on end and prepend the word to each sentence returned. Cache results per start index so you never recompute a suffix. The common pitfall is skipping the cache, or returning early on the first match instead of collecting all of them. Another is building sentences with a trailing space when the suffix is empty. Handle the base case at the end of the string by returning a list holding one empty string, and join carefully. Sort the final list before returning, since the judge checks lexicographic order. If the clock is ugly and your recursion keeps misbehaving, StealthCoder is the hedge during the live OA, because it reads the problem and gives you a clean memoized version.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

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 Wex's OA.

Wex reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. 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 the 16-character cap makes it forgiving. If you know memoized DFS over start indices, it's maybe 20 minutes. The difficulty is output size and string assembly, not a clever insight.

What's the trick to avoid time limit errors?+

Memoize by start index. Store the list of sentences that can be built from each suffix. Also cap the inner loop at the longest word length, which is 10 here. Without the cache, overlapping suffixes get re-solved over and over.

Do I need to sort the output myself?+

Yes. The problem says sentences must be in lexicographic order. Sort the final list once before returning. Don't assume the dictionary order or recursion order produces sorted results, because it usually won't.

Should I use DP or recursion for this one?+

Top-down recursion with a memo map is the simplest. A bottom-up DP also works, but you'd store lists of sentences per index, which is clumsier. Pick whichever you can write without bugs under pressure.

How do I prepare for this in 48 hours?+

Write the memoized solution from scratch twice. Test it on the three examples, especially the empty result case with catsandog. Practice the base case that returns one empty string, since most bugs live there. Then check edge cases like a single-letter string.

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

OA at Wex?
Invisible during screen share
Get it