AI Role: Next-Word Prediction from a Cleaned Corpus
Updated
Problem statement
Build a simple next-word predictor from a raw text corpus.
Clean each corpus record and each query context by converting ASCII uppercase letters to lowercase, then splitting at every character that is not a letter from a through z. Discard empty tokens. Do not remove stopwords or apply stemming.
Within each cleaned corpus record, every adjacent token pair contributes one observation from its first word to its second word. Count repeated pairs separately. Never create a pair across two corpus records.
For each string in contexts, use its last cleaned token as the current word. Return the following word with the greatest observed pair count. Break equal-count ties by lexicographically smaller following word. Return the empty string if the context has no cleaned token or its final token has no observed successor.
Return one prediction per context, in the original context order. This is a simple word-pair counting method, not a neural model.
Function
predictNextWords(corpus: String[], contexts: String[]) → String[]Examples
Example 1
corpus = ["I like tea.","I like coffee.","We like tea!"]contexts = ["I like","we","TEA","???"]return = ["tea","like","",""]tea follows like twice, while coffee follows it once. The word we is followed by like. No successor is observed for tea, and ??? has no cleaned token.
Example 2
corpus = ["A zoo","a ant","Last"]contexts = ["A","last","unknown",""]return = ["ant","","",""]The successors ant and zoo each follow a once. The lexicographically smaller ant wins. The remaining contexts have no observed successor.
Example 3
corpus = []contexts = ["hello"]return = [""]An empty corpus contains no observed pairs, so the prediction is empty.
Constraints
0 <= corpus.length <= 200.0 <= contexts.length <= 200.- Every string in
corpusandcontextshas length from0through1000. - Every input character is printable ASCII or a tab, newline, or carriage return.
More Apple Phone Screen questions
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Evaluate DivisionPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
- Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
- Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
- Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026
- Maintain a Consistent-Hashing RingPHONE SCREEN · Seen Oct 2026
- Count Nodes in a ChainPHONE SCREEN · Seen Oct 2026
- Run-Length DecodingPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Apply Operations to Make a String EmptyFlexport · PHONE SCREENFlexport interview questions
- Subdomain Visit CountsDatadog · PHONE SCREENDatadog interview questions
- Ransom NoteThe Walt Disney Company · OA · PHONE SCREENThe Walt Disney Company interview questions
- K-th Non-Repeating Character IndexBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Count One-Swap Number PairsCapital One · OACapital One interview questions
- How Many Sentences?Chubb · OAChubb interview questions
- Perfect SubstringsVisa · OAVisa interview questions
- Markov Transition Counts and ProbabilitiesRetool · ONSITE INTERVIEWRetool interview questions
- Cheating CodemakerJane Street · OAJane Street interview questions
- Count Equal-Frequency SubsequencesExpedia · OAExpedia interview questions
Apple interview experiences
- Apple SWE Phone Screen: Subarray Products: 1 Experience
- Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
- Apple AI Coding and Related Interviews: 17 Experiences
- Apple India SWE New Grad Interviews: 5 Experiences
- Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
- Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)