Implement Streaming Word Counter
NetflixMachine Learning Engineer · Technical ScreenLast 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:
add_text(text: str) -> None: Process a new text segment and update the frequency count for each word in the segment.get_count(word: str) -> int: Return how many times the given word has appeared so far.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
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.