Most-Frequent Next-Word Predictor
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Google OA reported in September 2026 looks like a friendly bigram counter, and then one edge case wrecks the naive version. You build a next-word predictor from adjacent token pairs, then answer up to 200000 queries fast. It's a hash-table problem with string comparison baked in. The trap is the tie-break and the no-successor case, and both fail quietly on small samples. If you blank mid-assessment, StealthCoder runs invisibly on your screen and gives you a working solution in real time. Know the shape first, though. It's shorter than it looks.
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
The trick is a map of maps. Outer key is the first token, inner map counts each successor. Walk the array once, bump count for pair (t[i], t[i+1]). While you update, track the current best successor for that first token: if the new count beats the best count, or ties it and the token is lexicographically smaller, replace it. Store the winner in a third map so each query is a single lookup, returning the empty string on a miss. The pitfall is updating the winner wrongly on ties. A token whose count rises can only beat the winner by being higher, or equal and smaller, so check both. Also, a single-token training array has zero pairs, so every query returns empty. Tokens are case-sensitive, so never lowercase anything. Don't scan the inner map per query, that blows the time budget. If the tie logic slips under pressure, StealthCoder is your hedge during the live OA.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
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 StealthCoderRelated leaked OAs
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.
Most-Frequent Next-Word Predictor FAQ
What's the trick in the Google next-word predictor problem?+
Count successors in a map of maps, and keep the best successor per token updated as you count. Each query then becomes one hash lookup. The best update rule is higher count wins, equal count goes to the lexicographically smaller token.
How hard is this one really?+
Easy to medium. The algorithm is a single pass with hash maps. Difficulty comes from the details: tie-breaking, missing queries returning an empty string, and not rescanning counts on every query with 200000 queries.
What edge cases should I test?+
Test a one-token training array, which has no pairs. Test a query that never appears or only appears as the last token. Test ties like Example 2. Test case-sensitive tokens such as Tea versus tea, which are different keys.
Can I compute the winner after counting instead of during?+
Yes. Build all counts first, then loop through each inner map once to pick the winner. Total work stays linear in the number of pairs. Updating during the pass is just slightly tidier, but both meet the requirement of precomputing before queries.
How do I prepare in 48 hours?+
Write this once from scratch with a nested hash map and the tie rule. Then do a couple of frequency-count problems with custom tie-breaks. Focus on string comparison in your language and on handling missing keys without exceptions.