Home โ€บ Interview Questions โ€บ Count how many times each word appears and store tโ€ฆ

Count how many times each word appears and store the result in a dictionary.

๐ŸŸก Medium Coding Junior level
1Times asked
Jun 2026Last seen
Jun 2026First seen

๐Ÿ’ก Model Answer

Split the string on whitespace to get a list of words. Iterate over the list and use a dictionary to keep a running count for each word. In Python you can use collections.Counter which abstracts this logic. Complexity is O(n) time and O(k) space, where n is the number of words and k is the number of unique words. Example:

python
from collections import Counter
text = "python is easy and python is powerful"
word_counts = Counter(text.split())
print(word_counts)  # Counter({'python': 2, 'is': 2, 'easy': 1, 'and': 1, 'powerful': 1})

This answer was generated by AI for study purposes. Use it as a starting point โ€” personalize it with your own experience.

๐ŸŽค Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers โ€” invisible to screen sharing.

Get Assisting AI โ€” Starts at โ‚น500