Stage 1 of 3: The likeliest next word
Background. A next-word predictor learns from example sentences which words tend to come right after which, and names the likeliest next word.
Implement NextWordPredictor in predictor.py: learn(sentences) and most_likely(word).
NextWordPredictor()starts with nothing learned. Each predictor keeps its own counts.learn(sentences)takes a list of sentences, each a list of words (non-empty strings), and returns nothing. It may be called any number of times, and every call adds to what earlier calls learned.- Two words form a pair when the second comes right after the first in the same sentence. Every occurrence counts: a pair twice in one sentence counts twice, and a word may follow itself. No pair spans two sentences, whether they arrive in one call or in two. A sentence may be empty or hold one word; neither forms a pair.
most_likely(word)returns the word that came right afterwordmost often. On a tie it returns the smallest of the tied words in Python's string order (whatminreturns: capital letters come before lowercase ones).- It returns
Nonewhen nothing ever came right afterword: a word never learned, or one that only ever ended its sentences. The end of a sentence is not a word, so a word that ended many sentences and was followed once by"x"answers"x".
Words are compared exactly as given: "The" and "the" are different words, and so are "rain" and "rain,".
Example
predictor = NextWordPredictor()
predictor.learn([["we", "bake", "bread"], ["we", "bake", "pies"], ["they", "bake", "bread"], ["we", "eat"]])
predictor.most_likely("bake") # "bread": twice, against once for "pies"
predictor.most_likely("we") # "bake"
predictor.most_likely("bread") # None: it only ends sentences
predictor.most_likely("pizza") # None: never learned
Notes
- Inputs are always valid.
- On the starter, all 11 tests in
tests/test_part1.pyfail.
Done when Submit goes green.
Constraints
- Python 3.12 standard library only
- Editable:
predictor.py - Read-only: all
tests/test_*.pyfiles - Inputs are always valid: a list of sentences, each a list of non-empty strings
What comes after stage 1
The next stages open once the tests for the one before them pass. Each raises the bar, the way an interviewer does in the room.
- Stage 2: Instant answers. Answers must stay instant while learning goes on and a word gathers tens of thousands of different successors.
- Stage 3: A weighted draw. Pick the next word at random, each as often as it was seen, and keep it quick while learning continues.
The files you get
- Yours to edit: predictor.py.
- Tests you can read and run: tests/test_part1.py. Hidden tests run on Submit.
The round: no AI, an assistant that only answers
Google runs this round without AI. Here the closest format is answer-only: the assistant answers your questions and reviews your code when you ask, the way an interviewer would, and never writes a line of it. The interviewer asks about your decisions as you go, and the report states the format. You can switch to the agent format when you press Begin.
Frequently asked, briefly answered
Is Next Word Predictor free?
It is a Pro problem. Free plan includes one complete problem, plus the first stage of several more, so you can see how a round works before you pay.
Which company asks something like this?
Google. It is modelled on Google's coding round, which runs without AI. Based on candidates' reports of Google interviews. Nyrion wrote this version, so the question you get may differ.
Do I bring my own AI tool?
No. The real round has no AI, so this one opens answer-only: an assistant that answers your questions and can't write code. You can switch to an agent that proposes changes when you press Begin.
What does the interviewer ask?
6 questions along the way about your own code, and one at the end about the whole session. A weak answer gets one follow up. The answers go into the report, which ends with a hire or no hire verdict.
Start the problem
Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the Pro plan starts at the first stage of this problem.
More like it: every practice problem, Google's interviews, the AI coding interview guide.