Weighted Next-Word Prediction From Training Sequences
Updated
Problem statement
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-basedticket.
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) → StringExamples
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
currentWordcontain English lettersA-Zora-zand have length from1through50. ticket == -1selects 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