KatInterview

Most Frequent Follower Lookup with Command Replay

Data Structure DesignAuto-graded · 21 test cases
EasyAuto-gradedData Structure Designhash-mapdesignstrings

Last updated

Simulate a train/predict command stream for a word-follower model, precomputing the top follower per word so each prediction is a single hash lookup, with a precise tie-break rule.

Word Follower Model

You are given a stream of commands for a tiny text model that learns which word most often comes right after another word. Process the commands in order and report the result of each one.

Commands

You receive two lists of equal length. commands[i] is the name of the i-th command, and payloads[i] is a list holding exactly one string, which is that command's argument.

  • "train": the argument is a piece of text. Split it on whitespace into words, preserving case. For every pair of neighbouring words (a, b) in that text, record that b followed a once. Counts accumulate across all train commands. Pairs are formed only inside a single text, never across the end of one text and the start of the next. Text with fewer than two words adds nothing.
  • "predict": the argument is a single word. Return the word that has followed it most often so far. If the word has never been seen with a follower, return None.

Output

Return a list with one entry per command, in order:

  • None for every train command.
  • The predicted word (a string) or None for every predict command.

Matching rules

  • Matching is exact and case-sensitive. "The" and "the" are different words. No punctuation stripping is done.
  • A word that appears only as the last word of a text, and nowhere else with a follower, has no followers, so predicting it gives None.
  • The query word is looked up as given. An empty string or any word never seen with a follower gives None.

Tie-break rule

If several followers share the highest count for a word, return the one that first appeared as a follower of that word across the entire history of train commands (earliest first occurrence, counting from the very first command). Later counts do not change this preference among tied words.

For example, if x first followed w before y did, and both currently have the same top count, the answer for w is x, even if y reached that count earlier than x did.

Aim to make each predict a constant-time lookup after training, not a scan over all followers.

Constraints

  • 1 <= len(commands) == len(payloads).
  • Each command is either "train" or "predict".
  • Each payloads[i] contains exactly one string.
  • Texts may be empty or contain only whitespace.
  • The total number of words across all texts fits comfortably in memory.
  • The number of distinct words is manageable.

Examples

Example 1

Input: replay_follower_commands(['train', 'predict', 'predict', 'predict'], [['the cat chased the mouse and the cat ate the cheese'], ['the'], ['cat'], ['unknown']])

Output: [None, 'cat', 'chased', None]

Example 2

Input: replay_follower_commands(['train', 'predict', 'predict', 'train', 'predict'], [['one two two three'], ['two'], ['one'], ['two three two'], ['two']])

Output: [None, 'two', 'two', None, 'three']

Example 3

Input: replay_follower_commands(['train', 'predict', 'predict'], [[''], ['anything'], ['']])

Output: [None, None, None]

Hints

Hint 1

Keep, for each word, a dictionary from follower to its count. Dictionaries remember insertion order, which helps with the tie-break rule.

Hint 2

To make predict O(1), store the current best follower for each word and update it while training, instead of searching at query time.

Hint 3

Counts only ever go up. When a follower's count rises to c, it becomes the best if c exceeds the best count, or if it ties and was first seen earlier than the current best. Store each follower's first-seen rank to compare.