Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Count Word Occurrences in Text

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Count Word Occurrences in Text. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Niantic telemetry and content pipelines may deliver text in chunks rather than as one complete string. Given an iterable stream of text chunks and a list of requested words, count how many times each requested word appears.

Matching is case-insensitive. A word is a contiguous sequence of ASCII letters, and any non-letter character separates words. A requested word is guaranteed to contain only ASCII letters. Do not concatenate the entire stream before processing it, and correctly handle a word split across two chunks.

Formal Specification

Implement count_stream_words(chunks, words), where chunks is an iterable of strings and words is a list of requested words. Return a dictionary mapping each requested word, normalized to lowercase, to its occurrence count. Include every requested word in the result, even when its count is zero. If the input list contains the same word more than once, return one key for that word.

Constraints

  • 1 <= len(words) <= 10^4
  • 1 <= len(word) <= 50 for every requested word
  • The total streamed text length is at most 10^7 characters
  • Chunks may be empty or split words at arbitrary positions
  • Only ASCII letters are part of a word

Function Signature

def count_stream_words(chunks, words):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output