Reported February 2026
Googledynamic programming

Minimum Dictionary Segments

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

Google reported this one in February 2026, and it looks like a string problem but it's really a shortest-path-on-indices problem in disguise. Each position in s is a node, each dictionary word that matches from there is an edge, and you want the fewest edges from 0 to the end. That's a 1D DP. If you've got an OA invite and 48 hours, this is the pattern to lock in. And if you blank mid-assessment, StealthCoder runs invisibly as a safety net and hands you the DP skeleton while you keep typing.

The problem

Given a string s and an array dictionary, split all of s into a sequence of dictionary words.
Return the minimum possible number of words in a complete split. Return -1 if no complete split exists. The empty string requires zero words.

Function
minimumDictionarySegments(s: String, dictionary: String[]) → int

Examples
Example 1
s = "applepie"
dictionary = ["apple","app","le","pie"]
return = 2
The split apple | pie uses two words. No dictionary word covers the entire string.
Example 2
s = "aaaa"
dictionary = ["a","aa","aaa"]
return = 2
Either a | aaa or aaa | a uses the minimum of two words.
Example 3
s = "catsandog"
dictionary = ["cats","dog","sand","and","cat"]
return = -1
Every possible prefix split leaves characters that cannot be covered by a dictionary word.

Constraints
0 <= s.length <= 2000
0 <= dictionary.length <= 2000
1 <= dictionary[i].length <= 50
s and every dictionary word contain lowercase English letters.
Duplicate dictionary words have no additional effect.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Define dp[i] as the minimum words needed to build the first i characters. Set dp[0] = 0 and everything else to infinity. For each i, try every dictionary word w that ends at i, meaning s.substring(i - len, i) equals w. Then dp[i] = min(dp[i], dp[i - len] + 1). Answer is dp[n] or -1 if it's still infinity. Put the words in a hash set and loop over lengths 1 to 50 instead of scanning all 2000 words. That gives about 2000 * 50 substring checks, which is fine. The common pitfall is greedy: taking the longest prefix fails on cases like catsandog. Another miss is forgetting the empty string returns 0, and that duplicates don't matter. If the DP recurrence slips away under pressure, StealthCoder is the hedge that surfaces it live.

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 Minimum Dictionary Segments 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

⏵ The honest play

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

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

Minimum Dictionary Segments FAQ

What's the trick to Minimum Dictionary Segments?+

Treat it as DP over prefix lengths. dp[i] is the fewest words to cover s[0..i). For each i, check word lengths up to 50 against a hash set and take dp[i - len] + 1. It's Word Break with a min instead of a boolean.

Why doesn't greedy longest-match work?+

Longest prefix can strand characters you can't cover later. In catsandog, grabbing cats then sand leaves og, and no other split rescues it. DP explores every valid split point, so it finds the true minimum or correctly reports -1.

What's the time complexity I should aim for?+

With s up to 2000 and words up to 50 characters, loop each end index and each length 1-50, check set membership. That's roughly n * 50 substring operations, each costing up to 50 characters. Comfortable for these constraints. Scanning all dictionary words per index is slower but still often passes.

What edge cases should I test before submitting?+

Empty s returns 0. Empty dictionary with nonempty s returns -1. Duplicate words are harmless. A string like aaaa with a, aa, aaa checks that you pick two words, not four. Also test a case where no full split exists.

How do I prepare for this in 48 hours?+

Write Word Break from scratch twice, then convert it to the minimum-count version. Practice the dp array init with infinity and the final -1 check. Once you can write the recurrence without looking, you're ready for this Google-style variant.

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