Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Find Word Occurrence Count

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

Your question is Find Word Occurrence Count. 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

Motional software may need lightweight text statistics when inspecting diagnostic messages. Given a sentence, count how many times each word appears.

A word is a maximal contiguous sequence of ASCII letters or digits. Matching is case-insensitive, and punctuation separates words but is not included in the result. Return a dictionary mapping each normalized lowercase word to its occurrence count. The order of dictionary entries does not matter.

Formal Specification

Implement count_word_occurrences(sentence), where sentence is a string. Return a dictionary whose keys are lowercase words and whose values are positive integer counts. If the sentence contains no words, return an empty dictionary.

Constraints

  • 0 <= len(sentence) <= 10^5
  • The input contains ASCII letters, digits, whitespace, and punctuation
  • Words contain at least one ASCII letter or digit
  • Matching is case-insensitive

Function Signature

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