Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Counting Word Occurrences

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

Your question is Counting Word Occurrences. 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

Glassdoor may need quick word-frequency summaries for a piece of review text. Given a text string and a list of target words, return how many times each target appears as a complete word.

Matching is case-insensitive. Treat any consecutive sequence of English letters or digits as one word, so punctuation separates words. Return a dictionary whose keys are the distinct target words in lowercase. A target that does not appear must still be included with count 0.

Formal Specification

Implement count_word_occurrences(text, words).

  • text is a string.
  • words is a list of strings, where each target contains only English letters and digits.
  • Return a dictionary mapping each distinct lowercase target word to its occurrence count in text.
  • Count complete tokens only. For example, job does not match jobs.

Constraints

  • 0 <= len(text) <= 10^5
  • 0 <= len(words) <= 10^4
  • 1 <= len(word) <= 30 for every target word
  • Text and target words contain ASCII letters, digits, and punctuation
  • Matching is case-insensitive and counts complete tokens only

Function Signature

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