Quick Overview

This question evaluates streaming data processing, string tokenization, and frequency-aggregation competencies, focusing on maintaining accurate counts over incremental text inputs using associative data structures.

Implement Streaming Word Counter

Company: Netflix

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement a class that records word frequencies from a stream of text. The class should support the following operations: 1. `add_text(text: str) -> None`: Process a new text segment and update the frequency count for each word in the segment. 2. `get_count(word: str) -> int`: Return how many times the given word has appeared so far. 3. `get_counts() -> dict`: Return the current mapping from words to their frequencies. Assumptions: - Words are separated by whitespace. - Empty tokens should be ignored. - Matching is case-sensitive unless you explicitly choose to normalize case. - You may use a hash map or dictionary to store counts. Example: ```text counter.add_text("hello world hello") counter.get_count("hello") -> 2 counter.get_count("world") -> 1 counter.get_count("missing") -> 0 ```

Quick Answer: This question evaluates streaming data processing, string tokenization, and frequency-aggregation competencies, focusing on maintaining accurate counts over incremental text inputs using associative data structures.

Simulate a streaming word counter. Because this platform expects a function instead of a class, you are given two lists: operations and values. Process them in order. Supported operations: - 'add_text': values[i] is a text segment. Split it by whitespace and add every non-empty word to the counter. - 'get_count': values[i] is a single word. Return how many times that word has appeared so far. - 'get_counts': values[i] is None. Return a snapshot dictionary of all current word frequencies. Return a list of results in the same order as the operations. Use None for each 'add_text' operation. Matching is case-sensitive.

Constraints

  • 1 <= len(operations) == len(values) <= 10^4
  • 0 <= total number of words across all 'add_text' operations <= 10^5
  • Words are separated by whitespace
  • Word matching is case-sensitive

Examples

Input: (['add_text', 'get_count', 'get_count', 'get_count'], ['hello world hello', 'hello', 'world', 'missing'])

Expected Output: [None, 2, 1, 0]

Explanation: After adding the text, 'hello' appears 2 times, 'world' appears 1 time, and 'missing' has not appeared.

Input: (['add_text', 'add_text', 'get_counts', 'get_count', 'get_counts'], ['a b a', 'b c', None, 'c', None])

Expected Output: [None, None, {'a': 2, 'b': 2, 'c': 1}, 1, {'a': 2, 'b': 2, 'c': 1}]

Explanation: The second add_text call increases 'b' and adds 'c'. get_counts should return the full current mapping each time.

Hints

  1. Use a dictionary to store the running frequency for each word.
  2. In Python, text.split() with no separator automatically handles multiple spaces and ignores empty tokens.

Loading coding console...