Katkat

Implement Streaming Word Counter

NetflixMachine Learning Engineer · Technical Screen

Last updated

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 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:

counter.add_text("hello world hello")
counter.get_count("hello") -> 2
counter.get_count("world") -> 1
counter.get_count("missing") -> 0