Reported September 2026
Googledynamic programming

Word Break

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

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

The edge case that kills a naive Word Break solution is the one where a greedy match looks right and then dead-ends. Google reported this one in September 2026, and it's the classic string segmentation problem. If "cats" or "cat" both match a prefix but only one leads anywhere, a greedy or plain recursive approach either lies or times out. You have an OA coming and you need the pattern, not a lecture. It's dynamic programming over string prefixes. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but you should walk in knowing the shape of the answer.

The problem

Given a string s and an array of dictionary words wordDict, return true if s can be split into a sequence of one or more dictionary words. Otherwise, return false.
All words in wordDict are available for the entire call, and a word may be reused multiple times. Dictionary membership uses exact string equality; duplicate entries do not change the result.

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

Examples
Example 1
s = "leetcode"
wordDict = ["leet","code"]
return = true
Split s as leet + code. Both pieces belong to wordDict.
Example 2
s = "catsandog"
wordDict = ["cats","dog","sand","and","cat"]
return = false
The prefixes cats and cat both lead to suffixes that cannot be fully segmented.
Example 3
s = "applepenapple"
wordDict = ["apple","pen"]
return = true
Split s as apple + pen + apple. Reusing apple is allowed.

Constraints
1 <= s.length <= 300.
1 <= wordDict.length <= 1000.
1 <= wordDict[i].length <= 20.
Every dictionary entry is non-empty.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: define dp[i] as true if the first i characters of s can be segmented. Set dp[0] to true. For each i from 1 to n, check every j less than i. If dp[j] is true and s[j:i] is in the dictionary set, dp[i] is true. Answer is dp[n]. Put wordDict into a hash set first so lookups are fast. The pitfall is plain recursion without memoization. On a string like many a's followed by a b, it explodes exponentially. Example 2, catsandog, is exactly that kind of dead-end trap. Greedy longest-match also fails there. Since words are at most 20 characters, you can cap the inner loop at 20 back from i, which trims work. With s up to 300, either version passes. If you freeze on the recurrence during the live OA, StealthCoder can surface the DP so you just verify and type it.

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 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. If you have time before the OA, drill that.

⏵ The honest play

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

Google 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 FAQ

What's the trick to Word Break?+

Think in prefixes. dp[i] means the first i characters can be fully split into dictionary words. You build it left to right, checking whether some earlier true position j plus the substring s[j:i] is a dictionary word. It's a yes/no table, not a search.

Why does greedy fail on Word Break?+

Picking the first or longest matching word can lock you into a dead end. In catsandog, both cats and cat match the start, but neither path finishes. Greedy never backtracks, so it returns wrong answers. DP or memoized recursion explores every valid split point.

How hard is this problem really for the Google OA?+

It's a medium. The DP is short once you see it, around ten lines. Most people lose points by skipping memoization or forgetting the hash set. If you've seen prefix DP before, it's quick. If not, the recurrence is the only thing to nail.

What's the time complexity I should state?+

With a hash set and the double loop, it's O(n^2) substring checks, and each substring hash costs up to O(n), so roughly O(n^3) worst case. Capping the inner loop at the max word length of 20 brings it near O(n * 20 * 20). Either passes for n of 300.

How do I prepare for this in 48 hours?+

Write Word Break from scratch twice without looking. Then do one memoized recursion version so you can switch styles. Test on catsandog and a string of repeated a's ending in b. Those two cases expose nearly every bug people ship.

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

OA at Google?
Invisible during screen share
Get it