← Questions
Count words in a text stream
0
I was asked this
No reports yet. Sign in to say where you were asked it.
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) -> Nonesplits the segment on whitespace and counts every non-empty word. -
get_count(word: str) -> intreturns how many times the word has appeared,0if 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,versushello)? - Can a word be split across two segments (
"hel"then"lo world")? - Should
get_countsreturn 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 andO(1)per lookup. - Whitespace splitting that ignores repeated spaces and empty tokens.
- Returning a copy from
get_countsso 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
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
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
