Home › Interview Questions › Count the frequency of each character in the strin…

Count the frequency of each character in the string and output the result as a dictionary.

🟢 Easy Coding Fresher level
1Times asked
Jun 2026Last seen
Jun 2026First seen

💡 Model Answer

Iterate over each character in the string, skipping spaces if desired, and increment a counter in a dictionary. Using collections.Counter simplifies this. Complexity is O(n) time and O(k) space, where n is the string length and k is the number of unique characters. Example:

python
from collections import Counter
text = "python is easy and python is powerful"
char_counts = Counter(c for c in text if c != ' ')
print(char_counts)
# Counter({'p': 2, 'y': 2, 't': 2, 'h': 2, 'o': 2, 'n': 2, 'i': 2, 's': 2, 'e': 2, 'a': 2, 'd': 1, 'l': 1, 'u': 1, 'r': 1, 'b': 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