🏷️ 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

ConceptWhy It Matters
collections.CounterPython library knowledge
dict.get() patternSafe dictionary access
String splittingText 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

Scroll to Top