Reported November 2020
Bloombergdynamic programming

Word Break with One Reconstruction

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

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

A hash set of dictionary words is what this Bloomberg OA question from November 2020 hinges on, and a DP table sits on top of it. You get a string and a word list, and you have to split the string into the fewest tokens. Ties go to the lexicographically smallest sentence, and no valid split means an empty string. It looks like classic Word Break, but the reconstruction and tie-break make it nastier. If you blank mid-assessment, StealthCoder runs invisibly as a safety net while you work through it. Here's the pattern so you don't need it.

The problem

Insert spaces into text so every token belongs to words. Return a valid sentence using the fewest tokens. If several use the same number, return the lexicographically smallest full sentence. Return the empty string when no segmentation exists.

Function
wordBreakSentence(text: String, words: String[]) → String

Examples
Example 1
text = "code"
words = ["c","d","co","e","x"]
return = "co d e"
co, d, and e reconstruct the text.

Constraints
0 <= text.length <= 1000.
Dictionary words are nonempty lowercase strings.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Put the words in a hash set. Then run DP over prefix positions: best[i] holds the best sentence for text[0..i], or null if it's unreachable. For each i, try every j < i where best[j] exists and text[j..i] is in the set. Compare candidates by token count first, then by the full sentence string. The pitfall is greedy tie-breaking. Picking the smallest last word doesn't guarantee the smallest sentence, so store whole sentences or compare carefully. With length up to 1000, storing strings is fine, but cap the inner loop at the longest dictionary word. Also handle empty text, where the answer is the empty string. In the example, "co d e" has 3 tokens, the minimum possible. If the live OA freezes you, StealthCoder can supply the full DP while you explain it.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Word Break with One Reconstruction 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 StealthCoder

Related leaked OAs

⏵ The honest play

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 with One Reconstruction FAQ

What's the trick in this Bloomberg word break variant?+

Use prefix DP with a hash set of words. For each end index, try every valid last word and keep the best sentence by fewest tokens, then lexicographic order. The tie-break is the only real twist over standard Word Break.

How hard is this really?+

Medium to medium-hard. Plain Word Break is easy to recognize. The difficulty is the double criterion, fewest tokens and then smallest sentence, plus returning the actual string instead of a boolean.

Can I just store the best previous word at each index?+

Risky. Lexicographic order on the whole sentence depends on earlier words, so a local choice can lose. Storing the full best sentence per prefix is safe at length 1000. Compare token count first, then the string.

What edge cases should I test?+

Empty text, text with no valid split, words that overlap like c and co, and ties between equal-token splits. The empty text case needs a clear decision about what you return. No segmentation also returns an empty string.

How do I prepare in 48 hours?+

Redo Word Break and Word Break II until the prefix DP is automatic. Then add a comparator for token count and lexicographic order. Write it once from scratch, and test with the example "code" giving "co d e".

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

OA at Bloomberg?
Invisible during screen share
Get it