← Questions

Count words in a text stream

0
NetflixCodingLast asked Apr 2026

A coding interview question asked in Netflix's technical phone screen, last asked Apr 2026. It tests hash maps, streams and strings.

Write a class that receives text from a stream, one segment at a time, and keeps a running count of how often each word has appeared so far.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • add_text(text: str) -> None splits the segment on whitespace and counts every non-empty word.
  • get_count(word: str) -> int returns how many times the word has appeared, 0 if never.
  • get_counts() -> dict[str, int] returns a snapshot of all counts.
  • Matching is case-sensitive, and words do not span segments.

Example

c = WordCounter()
c.add_text("hello world hello")
c.get_count("hello")    ->  2
c.get_count("world")    ->  1
c.get_count("missing")  ->  0

c = WordCounter()
c.add_text("a b a")
c.add_text("b   c")
c.get_counts()          ->  {"a": 2, "b": 2, "c": 1}

Constraints

  • Up to 10^4 calls and 10^5 words in total.

Clarifying questions

  • Should case and punctuation be normalized (Hello, versus hello)?
  • Can a word be split across two segments ("hel" then "lo world")?
  • Should get_counts return a copy or a live view?
  • Is the most frequent word, or the top k, needed?

What a strong answer covers

  • A hash map from word to count, O(length) per segment and O(1) per lookup.
  • Whitespace splitting that ignores repeated spaces and empty tokens.
  • Returning a copy from get_counts so callers cannot mutate internal state.
  • A clear choice on normalization, and tests for empty segments, repeated whitespace and case.

Follow-ups

  • Words can be split across segment boundaries: carry the trailing partial word to the next call.
  • Add top_k(k) that stays fast as counts change.
  • Make the counter thread-safe for many producers.
  • The vocabulary no longer fits in memory: approximate counts with a count-min sketch.

Where it's asked

  • Netflix · Technical phone screen · Last asked Apr 202660 min · Coding, practical problem solving, or SQL and pipeline design, depending on the org

What it tests

hash maps · streams · strings

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding