FastPrepWeighted Next-Word Prediction From Training Sequences
Problems/Weighted Next-Word Prediction From Training Sequences

Weighted Next-Word Prediction From Training Sequences

Google logoGoogle● MediumNEW GRADPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand Google L4 MLE report from July 2, 2025 gives arrays of sentences, predicting the most frequent word that comes right after, the example uppercase I -> AM, and a weighted-random follow-up with AM at probability 2/3 and WANT at probability 1/3. An earlier report also gives a follow-up with a large vocabulary. A -1 mode selector and a non-negative ticket keep the existing function and cover both tasks. We chose lexicographic ties, the order of weighted tickets, what happens with no next word, the signature, and the input limits. Letters are case-sensitive English, so the report's example still works. The denominator is the total count of next words for the current word, as the example shows, not every word in the corpus.

sentences contains training word sequences. Every adjacent pair within one sentence contributes one observed transition from the first word to the second. Sentence boundaries do not create transitions, and words are case-sensitive.

Given currentWord, count how often each word immediately follows it. The API supports the main prediction task and its weighted-sampling follow-up:

  • If ticket == -1, return the most frequent successor. Break equal-frequency ties by lexicographically smallest word.
  • If ticket >= 0, sort distinct successors lexicographically and give each word a consecutive block of tickets whose length equals its occurrence count. Return the word owning the zero-based ticket.

Lexicographic order uses English character order, with uppercase letters before lowercase letters. If no successor exists, return the empty string; ticket is then either -1 or 0.

A nonnegative ticket stands in for a random draw, so tests give the same result every time. Drawing it uniformly from all successor occurrences makes each word appear with probability equal to its count divided by that total.

Function

predictNextWord(sentences: String[][], currentWord: String, ticket: int) → String

Examples

Example 1

sentences = [["a","b","c"],["a","s","d"]]currentWord = "a"ticket = 0return = "b"

b and s each follow a once; lexicographic weighted tickets are [b,s].

Example 2

sentences = [["a","b"],["a","b"],["a","c"]]currentWord = "a"ticket = 1return = "b"

The tickets are [b,b,c], so ticket 1 still selects b.

Example 3

sentences = [["a","b"],["c"]]currentWord = "b"ticket = 0return = ""

b never appears before another word.

Example 4

sentences = [["I","AM","SAM"],["SAM","LIKES","ICECREAM"],["I","AM","TALL"],["I","WANT","COOKIE"]]currentWord = "I"ticket = -1return = "AM"

AM follows I twice; WANT follows I once, so the most-frequent prediction is AM.

Example 5

sentences = [["I","AM","SAM"],["SAM","LIKES","ICECREAM"],["I","AM","TALL"],["I","WANT","COOKIE"]]currentWord = "I"ticket = 2return = "WANT"

The weighted tickets are [AM, AM, WANT]. Tickets 0 and 1 choose AM, while ticket 2 chooses WANT. A uniform draw gives probabilities 2/3 and 1/3.

Constraints

  • 0 <= sentences.length <= 100000.
  • The total number of words is at most 500000. A sentence may be empty.
  • Words and currentWord contain English letters A-Z or a-z and have length from 1 through 50.
  • ticket == -1 selects the most-frequent mode. Otherwise, if successors exist, 0 <= ticket < total successor occurrences; if none exist, ticket == 0.

More Google Phone Screen questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
  • Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Longest Equal-Frequency SubstringDRW · PHONE SCREENDRW interview questions
  • Shortest Digit Prefix for Target MultisetsVisa · OAVisa interview questions
  • Longest Balanced Substring After One SwapTekion · OATekion interview questions
  • Count Special StringsAmazon · OA
  • Longest Substring with Even OccurrencesDeloitte · OADeloitte interview questions
  • Extraordinary SubstringsPalantir · OAPalantir interview questions
  • Number of Divisible Substrings (For MLE)Wayfair · OAWayfair interview questions
  • Classify Password StrengthHackerRank · OA · PHONE SCREEN · ONSITE INTERVIEWHackerRank interview questions
  • Highest Bid Campaign MatcherStackAdapt · ONSITE INTERVIEWStackAdapt interview questions
  • Group AnagramsAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions

Interview reports that mention this question

  • Google L4 ML Loops and Team Matching: 8 Experiences (Part 2 of 4)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public String predictNextWord(String[][] sentences, String currentWord, int ticket) {
        // write your code here
    }
}
Checking account…