KatInterview

Most Likely Follower Lookup via Operation Log

Data Structure DesignAuto-graded · 13 test cases
MediumAuto-gradedData Structure Designhash-mapdesigncounting

Last updated

Simulate a bigram next-word model from an operation log, returning the most frequent follower with lexicographic tie-breaking and an empty string when none exists.

Follower Prediction Simulator

You are given a log of operations to run against a small "next word" model. The model learns from several token sequences and answers which token most often comes right after a given token.

Learning rule

Two tokens form a transition (a, b) when b comes immediately after a inside the same sequence. Transitions never cross sequence boundaries: the last token of one sequence is not linked to the first token of the next. Each occurrence of a transition adds 1 to its count, whether it repeats within one sequence or across several.

Operations

operations and arguments have the same length. arguments[i] is the argument list for operations[i]. Process them in order.

  • "WordPredictor": builds the model. Use arguments[i][0] as the training collection. Each element of that collection is one sequence, read element by element. If a sequence is a plain string, its individual characters are its elements. Append None to the output. Constructing the model again replaces the old one.
  • "predict": arguments[i][0] is the query token. Append the token that most frequently follows it across all training data. If several followers tie for the highest count, append the lexicographically smallest of them. If the query token has no follower (it was never seen, or it only ever appears at the end of sequences), append "". If no model has been built yet, append "".

Any other operation name should raise ValueError.

Return the list of outputs, one per operation, in order.

Notes

  • Empty sequences and one-element sequences produce no transitions.
  • A token can follow itself (for example, ["go", "go"] gives the transition go -> go).
  • Queries are compared exactly against the tokens as they were read from the training data.

Constraints

  • 1 <= len(operations) == len(arguments) <= 10^5 + 1
  • Training collection has at most 10^4 sequences, each with at most 10^3 elements, and at most 2 * 10^5 elements in total.
  • Tokens consist of lowercase English letters.
  • Up to 10^5 predict operations, each expected to be answered in average constant time after construction.

Examples

Example 1

Input: simulate_next_word_model(['WordPredictor', 'predict', 'predict', 'predict', 'predict'], [[['the', 'quick', 'brown', 'fox'], ['the', 'lazy', 'dog'], ['the', 'quick', 'rabbit']], ['the'], ['quick'], ['dog'], ['cat']])

Output: [None, '', '', '', '']

Hints

Hint 1

Only adjacent elements inside one sequence matter, so loop over each sequence separately and never over a flattened list.

Hint 2

Keep a map from each token to a map of follower counts. Once all counts are known, the best follower of each token can be computed a single time.

Hint 3

Pick the best follower with the key (-count, word), then answer each query with one dictionary lookup that defaults to an empty string.