PracHub
QuestionsPremiumLearningGuidesCheatsheetNEW

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.

  • medium
  • Netflix
  • Coding & Algorithms
  • Machine Learning Engineer

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.

Input: (['add_text', 'add_text', 'get_count', 'get_count', 'get_counts'], [' ', 'Hi hi HI ', 'Hi', 'hi', None])

Expected Output: [None, None, 1, 1, {'Hi': 1, 'hi': 1, 'HI': 1}]

Explanation: Whitespace-only text adds nothing, and matching is case-sensitive, so 'Hi', 'hi', and 'HI' are counted separately.

Input: (['get_counts', 'get_count', 'add_text', 'get_counts'], [None, 'anything', '', None])

Expected Output: [{}, 0, None, {}]

Explanation: Before any text is added, the counter is empty. Adding an empty string does not change the counts.

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.
Last updated: May 12, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Cache, Undo, and DFS - Netflix
  • Implement ordering and undo executor - Netflix (medium)
  • Implement Caches, Undo, and Traversal - Netflix
  • Compute subtree sums with tree DFS - Netflix (hard)
  • Solve sliding-window and disjoint-string-pairs tasks - Netflix (medium)