π·οΈ Category: Strings / HashMap | π― Difficulty: Easy | π₯ Frequency: β
β
β
β
β
π Problem Statement
Given a string, count the frequency of each word(Word Frequency) and return a dictionary.
π₯ Input β "the cat sat on the mat the cat"
π€ Output β {'the': 3, 'cat': 2, 'sat': 1, 'on': 1, 'mat': 1}
π₯ Input β "apple orange apple banana orange apple"
π€ Output β {'apple': 3, 'orange': 2, 'banana': 1}
π§ What the Interviewer is Testing
| Concept | Why It Matters |
|---|---|
| collections.Counter | Python library knowledge |
| dict.get() pattern | Safe dictionary access |
| String splitting | Text processing skills |
π‘ Approach
1. Split string into words β list of words
2. Count each word using Counter or manual dict
3. Return result dictionary
β Optimal Solution β Using Counter
from collections import Counter
def word_frequency(s: str) -> dict:
words = s.lower().split()
return dict(Counter(words))
# ββ Test Cases βββββββββββββββββββββββββββββββββ
print(word_frequency("the cat sat on the mat the cat"))
# β
{'the': 3, 'cat': 2, 'sat': 1, 'on': 1, 'mat': 1}
π Manual Dictionary Approach
def word_frequency_manual(s: str) -> dict:
freq = {}
for word in s.lower().split():
freq[word] = freq.get(word, 0) + 1
return freq
π¬ Pro tip: Know BOTH approaches. Counter shows library knowledge, manual approach shows you understand whatβs happening underneath.
π¬ Pro tip: Know BOTH approaches. Counter shows library knowledge, manual approach shows you understand whatβs happening underneath.
π Complexity Analysis
βββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β Time O(n) β n = number of words in string β
β Space O(k) β k = number of unique words β
βββββββββββββββ΄βββββββββββββββββββββββββββββββββββββ
π€ How to Explain in the Interview
βI split the string into words, lowercase for consistency, then use Counter from collections to count each wordβs frequency. Counter is O(n) and returns a dictionary-like object. I can also do it manually with dict.get() if Counter isnβt allowed.β
βI split the string into words, lowercase for consistency, then use Counter from collections to count each wordβs frequency. Counter is O(n) and returns a dictionary-like object. I can also do it manually with dict.get() if Counter isnβt allowed.β
π Common Follow-up Questions
Q1 β Top 3 most frequent words?
from collections import Counter
def top_n_words(s: str, n: int) -> list:
return Counter(s.lower().split()).most_common(n)
# [('the', 3), ('cat', 2), ('sat', 1)]
Q2 β Ignore stop words?
stop_words = {'the', 'on', 'a', 'an', 'is'}
def word_freq_filtered(s: str) -> dict:
words = [w for w in s.lower().split() if w not in stop_words]
return dict(Counter(words))
π Set 1 of 4 β Python Interview Prep Series β devprepguide.com
