Reported December 2022
Bloomberghash table

Most-Frequent Next-Word Predictor

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

Bloomberg reported this one in December 2022, and the input size is the whole story. Two arrays of up to 200000 tokens each means you can't rescan the training data for every query. The task is a next-word predictor: count what follows each token, pick the most frequent successor, break ties alphabetically. It's a hash-table counting problem dressed up as NLP. If you have the OA coming up, know the shape before you open it. And if you blank on the nested map or the tie-break, StealthCoder runs invisibly as a safety net during the live assessment.

The problem

Train a next-word predictor from one ordered array of case-sensitive tokens. Every adjacent pair contributes one observation from its first token to its second.
For each query token, return the observed following token with highest frequency. Break frequency ties by lexicographically smaller token. Return the empty string when the query has no observed successor. Precompute the winning successor while processing the training data so each query is answered in average O(1) time.

Function
predictNextWords(trainingTokens: String[], queries: String[]) → String[]

Examples
Example 1
trainingTokens = ["i","like","tea","i","like","coffee","i","like","tea"]
queries = ["i","like"]
return = ["like","tea"]
The word after i is always like, while tea follows like twice and coffee once.
Example 2
trainingTokens = ["x","z","x","a"]
queries = ["x"]
return = ["a"]
The successors a and z tie, so the lexicographically smaller a wins.

Constraints
1 <= trainingTokens.length, queries.length <= 200000
Every token is a nonempty printable ASCII string of length at most 40.
The total number of token characters across both arrays is at most 1000000.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Brute force scans the training array once per query. That's 200000 times 200000, which dies. The fix is to build a map from token to a map of successor counts in one pass over adjacent pairs. While updating a count, compare it against the current best for that token. Replace the best if the new count is higher, or equal with a lexicographically smaller successor. Store the winner in a second map so each query is a single lookup. Queries with no entry return the empty string. The common pitfall is the tie-break only running when counts are equal, or forgetting that a successor's count can rise later and overtake the leader. Updating the best on every increment handles that. Keep comparisons case-sensitive, plain string compare. If the nested-map bookkeeping slips under pressure, StealthCoder is the hedge on the live OA.

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 Most-Frequent Next-Word Predictor 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 Bloomberg's OA.

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

Most-Frequent Next-Word Predictor FAQ

What's the trick in the Bloomberg next-word predictor problem?+

Build a map of token to successor counts in one pass over adjacent pairs. Track the best successor per token as you update counts, so queries become O(1) lookups. Don't recompute the winner at query time, the problem explicitly asks you to precompute it.

How do I handle ties correctly?+

When a successor's new count equals the current best count, compare the strings and keep the smaller one. When it exceeds the best count, replace the best. Do this on every increment, not just at the end, so a late-rising successor can still take the lead.

How hard is this really?+

Easy to medium. There's no tricky algorithm, just hash maps and careful bookkeeping. The risk is off-by-one on adjacent pairs, missing the tie-break, or scanning per query and timing out on the 200000 limits.

What should I return for a query with no successor?+

Return the empty string. That covers tokens never seen in training and tokens that only appear as the last element, since the last token has no following pair. A map lookup with a default of empty string handles both cases cleanly.

How do I prepare for this in 48 hours?+

Practice nested hash maps and counting with a running best. Write this exact function once from scratch, test Example 2 for the tie-break, and check the last-token edge case. Also confirm your language's string comparison is plain lexicographic and case-sensitive.

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