Reported June 2026
Runwayhash table

N Gram Next Token Prediction

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

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

Runway reported this one in June 2026, and the input sizes are the whole story. Up to 10^4 queries against 2 * 10^5 training tokens means rescanning the corpus per query dies fast. The task is an n-gram next token predictor: build counts once, answer lookups instantly. It's a hash-table problem wearing an NLP costume. If you blank on the nested map structure during the live OA, StealthCoder sits invisibly on your screen as a safety net. Know the shape first and you probably won't need it.

The problem

Fit an n-gram model from the supplied training sentences, then predict one next token for each query context.
Split each sentence and context on whitespace without crossing sentence boundaries. Use the final n - 1 context tokens. Choose the observed successor with the highest count, breaking ties by lexicographically smaller token. Return <unknown> when a complete context was not observed. For n = 1, use global token counts and ignore each context.

Function
predictNextTokens(trainingSentences: String[], n: int, contexts: String[]) → String[]

Examples
Example 1
trainingSentences = ["the cat sat","the cat slept","the dog sat"]
n = 2
contexts = ["the","cat","dog"]
return = ["cat","sat","sat"]
For "the", cat appears twice; the other contexts each have one most frequent successor.
Example 2
trainingSentences = ["red blue red","blue red green"]
n = 1
contexts = ["","anything"]
return = ["red","red"]
A unigram model ignores context and predicts the most frequent token.
Example 3
trainingSentences = ["a b c"]
n = 3
contexts = ["a b","b c","a"]
return = ["c","<unknown>","<unknown>"]
Only the complete observed two-token context has a successor.

Constraints
1 &le; trainingSentences.length, contexts.length &le; 10^4.
1 &le; n &le; 5.
The total number of whitespace-separated tokens is at most 2 * 10^5.
Tokens are non-empty case-sensitive strings.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is one pass over the training data. For each sentence, split on whitespace, slide a window of size n, and use the first n-1 tokens as the key and the last token as the successor. Store a map from context string to a map of successor counts. For n = 1 the key is the empty string, so global counts fall out for free, and you ignore the query context. At query time, take the last n-1 tokens of the context, join them, and look up the key. Pick the max count, ties broken by the smaller token. The pitfalls: windows must never cross sentence boundaries, a context shorter than n-1 tokens returns <unknown>, and the join key must be consistent between training and queries. Compute the best successor lazily or once per key. StealthCoder is your hedge if the tie-break logic slips under pressure.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill N Gram Next Token Prediction 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder
⏵ The honest play

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

Runway reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

N Gram Next Token Prediction FAQ

What's the trick in the Runway N Gram Next Token Prediction problem?+

Preprocess once. Slide an n-sized window over each sentence, map the first n-1 tokens to a count map of the final token, then answer each query with a hash lookup. Brute force rescanning per query is too slow at 10^4 queries.

How do I handle n = 1?+

Use the empty context as the single key. Every token in every sentence increments its count under that key. Then every query ignores its context and returns the most frequent token, with ties broken by the lexicographically smaller one.

What are the common edge cases?+

Context with fewer than n-1 tokens, contexts never seen in training, windows crossing sentence boundaries, and ties in counts. Also extra whitespace in contexts. Splitting on whitespace and taking the last n-1 tokens handles most of these cleanly.

How hard is this really?+

Easy to medium. There's no advanced algorithm, just careful hash map work and a tie-break rule. Most failures come from sloppy key construction or off-by-one window bounds, not from the idea itself.

How do I prepare in 48 hours?+

Write it from scratch twice in your language of choice. Use a map of maps, test the three given examples, then add cases for ties and short contexts. Practice the max-with-tie-break loop until it's automatic.

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

OA at Runway?
Invisible during screen share
Get it