KatInterview

Most Likely Follower Lookup Over Streaming Sentences

Data Structure DesignAuto-graded · 20 test cases
EasyAuto-gradedData Structure Designhash-mapstringssimulation

Last updated

Process a stream of add and query operations over sentences, tracking adjacent-word counts and returning the most frequent follower with lexicographic tie-breaking.

You are building a tiny autocomplete engine that suggests which word most often comes right after a given word. The engine is driven by a sequence of operations that is replayed against it.

Task

Write a function that simulates the engine. You receive two parallel lists, operations and arguments. The i-th entry of operations names an operation, and the i-th entry of arguments is the list of arguments for that operation. The engine starts empty.

Two operations exist:

  • "add_sentences": its argument list holds one item, a list of sentence strings. Split each sentence on whitespace to get its words. For every pair of neighbouring words (left, right) inside the same sentence, record that right followed left once. Pairs never span two different sentences. This operation produces None in the output.
  • "get_next_word": its argument list holds one item, a string word. The result is the word that has followed word the most times across all sentences added so far. If several words are tied for the highest count, return the lexicographically smallest of them. If word has never been followed by any word (it was never seen, or it only ever appeared as the last word of a sentence, or only in single-word sentences), the result is None.

Return a list with one entry per operation, in order: None for each "add_sentences", and the answer for each "get_next_word".

Details

  • Words are compared exactly, so "Cat" and "cat" are different words.
  • Splitting uses any run of whitespace as a separator, and leading or trailing whitespace is ignored. Empty sentences and one-word sentences contribute no pairs.
  • A word can follow itself (for example "go go" records go followed by go).
  • Counts accumulate across multiple "add_sentences" calls, and a query reflects everything added before it.
  • Lexicographic order is the standard string order of Python.
  • Queries may be frequent, so aim for fast lookups.

Constraints

  • The total number of words across all sentences is at most 10^5.
  • operations and arguments have the same length.
  • Each operation is either "add_sentences" or "get_next_word".
  • Words are non-empty strings without internal whitespace.

Examples

Example 1

Input: simulate_follower_engine(['add_sentences', 'get_next_word', 'get_next_word', 'get_next_word'], [[['cat sat on mat', 'cat sat on rug', 'dog sat on mat']], ['cat'], ['sat'], ['on']])

Output: [None, 'sat', 'on', 'mat']

Example 2

Input: simulate_follower_engine(['add_sentences', 'get_next_word', 'get_next_word', 'get_next_word', 'get_next_word'], [[['a b c', 'a b d', 'a b d', 'x y']], ['a'], ['b'], ['c'], ['z']])

Output: [None, 'b', 'd', None, None]

Example 3

Input: simulate_follower_engine(['add_sentences', 'get_next_word', 'add_sentences', 'get_next_word'], [[['one two']], ['one'], [['one three', 'one three']], ['one']])

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

Hints

Hint 1

Only adjacent words inside a single sentence count; build pairs per sentence so the end of one sentence is never linked to the start of the next.

Hint 2

Keep a nested map from a word to a map of follower counts. A query that rescans all followers can be slow if queries are frequent.

Hint 3

Because counts only ever increase, you can maintain the current best follower for each word as you add pairs: after incrementing a pair, compare the follower against the stored best by (higher count, then smaller word).