KatInterview

Phrase Completion Engine with Windowed Context Counts

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designhash-mapn-gramsimulation

Last updated

Simulate a command sequence on a context-based next-word model, using hash maps of counts and a lexicographic tie-break.

Phrase Completion Engine with Windowed Context Counts

You are building a tiny text-completion engine. It learns from sentences and, given a short sequence of words (the context), suggests the word that most often comes right after that context.

The engine has a fixed window size window. Every context has exactly window words.

Operations

You are given the window size and two parallel lists: commands and payloads. The i-th command is applied to the engine in order, using payloads[i] as its argument list.

  • "train": payloads[i][0] is a list of sentence strings. Each sentence is split into words on whitespace, and empty tokens are ignored, so repeated spaces do not matter. For each sentence on its own, look at every run of window consecutive words. The word immediately after that run is its follower. Add one to the count of that (context, follower) pair. Sentences are processed independently, so a context never spans two sentences. A sentence with window words or fewer adds nothing. Counts accumulate across multiple "train" commands.
  • "predict": payloads[i][0] is a list of words, the query context. Return the follower with the highest count for that context.
    • If several followers share the highest count, return the lexicographically smallest one (standard string comparison, case-sensitive, no normalization).
    • If the query's length is not equal to window, or the context was never seen in training, return None.

Words are compared exactly as given, so "The" and "the" are different words.

Output

Return a list with one entry per command, in order. A "train" command yields None. A "predict" command yields the suggested word, or None as described above.

Constraints

  • window is a non-negative integer. With window = 0 the context is empty, and the prediction is the most frequent word overall (an empty query list is then a valid context).
  • len(commands) == len(payloads).
  • Every command is either "train" or "predict".
  • Sentences contain only words separated by whitespace. Total word count across all training is moderate, so a single pass over each sentence is expected.
  • Words are non-empty strings after splitting.
  • Prediction should be a direct lookup on the context, not a scan over all stored contexts.

Examples

Example 1

Input: simulate_phrase_completer(2, ['train', 'predict', 'predict', 'predict'], [[['the cat sat on the mat', 'the cat ate the fish']], [['the', 'cat']], [['cat', 'sat']], [['the']]])

Output: [None, 'ate', 'on', None]

Example 2

Input: simulate_phrase_completer(1, ['train', 'predict', 'predict', 'predict'], [[['a b c', 'a b d', 'a b c']], [['a']], [['b']], [['x']]])

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

Hints

Hint 1

Treat each run of `window` consecutive words in a sentence as a key (a tuple works well) and keep a count per follower word under that key.

Hint 2

Split with the default whitespace split so extra spaces vanish, and only slide the window over positions where a following word exists. Never join words from different sentences.

Hint 3

To pick the answer, choose the follower with the largest count and break ties with the smaller string, for example by minimizing the pair (-count, word). Return None if the query length differs from window or the key is missing.