Reported February 2026
Googlehash table

Frequency-Weighted Next-Word Sampling

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 mistake that sinks a first attempt on this Google OA, reported February 2026, is building pairs across sequence boundaries or scanning successors linearly per query. Frequency-Weighted Next-Word Sampling looks like a language-model problem, but it's a hash table plus prefix sums. You count adjacent pairs inside each sequence only, sort each word's successors, and map a ticket to a range. If you blank on the ordering or the boundary rule, StealthCoder is the invisible safety net that reads the problem on screen and hands you a working solution while the clock runs.

The problem

Train a next-word model from several token sequences. Within each sequence, every adjacent pair contributes one observation from its first token to its second; sequence boundaries never create observations.
For query queries[i], let its observed successors be sorted lexicographically. Their frequencies form consecutive weighted ranges in that order. If the query has total successor observations, tickets[i] is an integer from 0 through total - 1; return the successor whose range contains that ticket. Thus a successor observed f times owns exactly f tickets.
If a query has no observed successor, its ticket is 0 and the answer is the empty string. Return one sampled word per query.

Function
sampleNextWords(training: String[][], queries: String[], tickets: int[]) → String[]

Examples
Example 1
training = [["a","b","c"],["a","s","d"],["a","b","d"]]
queries = ["a","b","x"]
tickets = [2,1,0]
return = ["s","d",""]
After a, lexicographic successor ranges are b:[0,2) and s:[2,3). After b, c owns ticket 0 and d owns ticket 1. The unseen query x returns an empty string.
Example 2
training = [["go","left"],["go","right"],["go","right"]]
queries = ["go","go","go"]
tickets = [0,1,2]
return = ["left","right","right"]
Lexicographic order gives left the first ticket and right the next two tickets, matching their frequencies one and two.

Constraints
1 <= training.length, queries.length <= 100000
Each training sequence contains between 1 and 100000 tokens.
The total number of training tokens and query tokens is at most 300000.
Every token contains 1 to 40 printable ASCII characters.
queries.length == tickets.length.
For a query with successor count total > 0, 0 <= tickets[i] < total; otherwise tickets[i] == 0.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build a map from word to a map of successor to count. Loop each sequence from index 0 to length minus 2, and count pair (seq[i], seq[i+1]). Never connect the last token of one sequence to the first of the next. Then, for each word, sort successors lexicographically and build a prefix-sum array of counts, plus the total. Cache this lazily or build it up front. For each query, a missing word returns the empty string. Otherwise binary search the prefix array for the first prefix sum strictly greater than the ticket. The common pitfall is rescanning the successor list per query, which blows up with 100000 queries on a hot word. Another is using less-than-or-equal off by one at range edges. Sort once per word, not per query. If the logic slips under pressure, StealthCoder is the hedge during the live OA. Total cost is roughly O(N log N) for sorting plus O(log k) per query.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Frequency-Weighted Next-Word Sampling 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. If you're reading this with an OA window open, you're who this was built for.

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. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Frequency-Weighted Next-Word Sampling FAQ

How hard is this Google question really?+

Medium. The idea is simple, a nested count map plus prefix sums. The difficulty is in the details: boundary pairs, lexicographic order, and the ticket-to-range mapping. With 300000 total tokens, a naive per-query scan can time out, so efficiency matters more than cleverness.

What's the trick to the ticket lookup?+

Sort each word's successors, then build cumulative counts. The answer is the first successor whose cumulative count is greater than the ticket. Binary search on that array gives O(log k) per query. Example 1 shows it: after a, b covers [0,2) and s covers [2,3).

What edge cases break most submissions?+

Crossing sequence boundaries, single-token sequences that produce no pairs, and queries for words that were never a first token. Those must return an empty string. Also watch tokens with printable ASCII like spaces, so don't split on whitespace.

Do I need to sort the whole vocabulary?+

No. Sort only the successors of each word that appears as a first token. Sorting per word keeps total work near N log N. You can also do it lazily, only for queried words, and cache the result so repeated queries stay fast.

How do I prepare for this in 48 hours?+

Practice nested hash maps, prefix sums, and binary search for a first value above a target. Write the solution once from scratch and test both examples by hand. Check the empty-successor case and a duplicate-heavy case like the go, left, right example.

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