Word Frequency Counter
Python data engineering interview problem. Difficulty: beginner. Pattern: Dictionaries. About 10 minutes. Free to practice.
Count how many times each whitespace-separated word appears. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
Counting how often each word appears is one of the most common tasks in data work. It is used for search, for text analysis and for finding the most popular items. Write a function word_frequency(text) that takes a string, splits it into words at the spaces, and returns a dictionary. Each key is a word, and each value is the number of times that word appears. For 'a b a' the answer is {'a': 2, 'b': 1}. Keys should be in the order the words first appear. Keep the print lines in the editor.
Requirements
- Return a dictionary where each key is a different word.
- Each value is the number of times that word appears in the text.
- Keys are in first-seen order.
- Split the text on whitespace to get the words.
Constraints
- Words are separated by spaces.
- text is a string, and the words are compared exactly as written.
Examples
Input: word_frequency('a b a') Output: {'a': 2, 'b': 1} Second 'a' increments the count.
Topics: lakebench, python, counting, strings.
More Python interview questions · All interview problems · Learn data engineering
Word Frequency Counter
Interview-style drill: Count how many times each whitespace-separated word appears.
Implement `word_frequency(text: str) -> dict` counting whitespace-separated words. Example: `"a b a"` → `{'a': 2, 'b': 1}`. Keep the harness.