Word Break
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that kills a naive Word Break solution is a greedy match that looks right and dead-ends later. "catsandog" is the example that exposes it. Bloomberg candidates reported this one in July 2022, and it's a classic string DP problem dressed up as a dictionary lookup. If you grab "cats" first, you never recover. The fix is small once you see it. If you blank during the live OA, StealthCoder runs invisibly as a safety net and hands you the solution, but the pattern is short enough to learn tonight.
The problem
Given a string s and an array of distinct dictionary words wordDict, return true if s can be split into a sequence of one or more dictionary words. A dictionary word may be reused any number of times. Function wordBreak(s: String, wordDict: String[]) → boolean Examples Example 1 s = "leetcode" wordDict = ["leet","code"] return = true The string splits as leet + code. Example 2 s = "applepenapple" wordDict = ["apple","pen"] return = true The word apple is reused in apple + pen + apple. Example 3 s = "catsandog" wordDict = ["cats","dog","sand","and","cat"] return = false No sequence of dictionary words covers the entire string. Constraints 1 <= s.length <= 300. 1 <= wordDict.length <= 1000. 1 <= wordDict[i].length <= 20. s and every dictionary word contain only lowercase English letters. All dictionary words are distinct.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is dynamic programming over prefixes. Let dp[i] be true if the first i characters can be segmented. Set dp[0] = true. For each i, check each j < i where dp[j] is true and s[j:i] is in the dictionary. Put the words in a hash set for O(1) lookups. Answer is dp[n]. Greedy fails because the first matching word isn't always part of a valid split, as "catsandog" shows. Plain recursion without memoization blows up exponentially. Since words are at most 20 characters, only check j within 20 of i, which drops the work to about n times 20 substring checks. Common pitfalls: forgetting dp[0] = true, off-by-one on slice bounds, and scanning the whole dictionary per position. If the logic slips under pressure, StealthCoder is the hedge on the live OA.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Word Break 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as word break. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Word Break FAQ
How hard is Word Break really for the Bloomberg OA?+
It's medium. The DP is only a few lines, but people stall because they try greedy or raw recursion first. If you know the prefix-boolean array idea, you can write it in ten minutes. The difficulty is recognizing the pattern, not the code.
What's the trick to Word Break?+
Define dp[i] as whether the first i characters can be built from dictionary words. Seed dp[0] as true. For each end index i, look for a split point j where dp[j] is true and s[j:i] is in the set. Return dp[n].
Why does the greedy approach fail?+
Greedy takes the first word that matches and commits. With "catsandog" and words like cats, cat, and, sand, dog, taking "cats" leaves "andog", which fails. You need to consider every split, which is why DP or memoized recursion is required.
What's the time complexity I should state?+
Using a hash set and checking substrings, it's O(n^2) splits times substring cost, so roughly O(n^3) worst case for n up to 300. Cap the inner loop at the max word length of 20 and it's about O(n * 20 * 20). Either is fine for these constraints.
How do I prepare for this in 48 hours?+
Write Word Break from scratch twice without looking, once bottom-up DP and once top-down with memoization. Then test on "catsandog" and a single-character string. Also skim the follow-up where you return all sentences, since interviewers sometimes extend it.