cswork / libraryOpen practice workspace

Coding questions · Netflix

Implement Streaming Word Counter

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

Implement Streaming Word Counter

Simulate a streaming word counter. 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

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.