Challenge - 5 Problems
Character Frequency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of character frequency count using dictionary
What is the output of the following Python code that counts character frequency in a string?
DSA Python
text = "hello" freq = {} for ch in text: freq[ch] = freq.get(ch, 0) + 1 print(freq)
Attempts:
2 left
💡 Hint
Count how many times each letter appears in the word 'hello'.
✗ Incorrect
The code loops through each character in the string 'hello'. It adds 1 to the count for each character in the dictionary. The letter 'l' appears twice, so its count is 2. All other letters appear once.
🧠 Conceptual
intermediate1:30remaining
Understanding character frequency counting with default dictionary
Which Python data structure is best suited to count character frequencies without manually checking if a key exists?
Attempts:
2 left
💡 Hint
Think about a dictionary that automatically gives zero for missing keys.
✗ Incorrect
defaultdict(int) automatically initializes missing keys with 0, making counting easier without manual checks.
🔧 Debug
advanced2:00remaining
Identify the error in character frequency counting code
What error does the following code produce when counting characters in 'test'?
DSA Python
text = "test" freq = {} for ch in text: freq[ch] += 1 print(freq)
Attempts:
2 left
💡 Hint
Check what happens when you try to add 1 to a key that does not exist yet.
✗ Incorrect
The dictionary freq is empty initially. Trying to do freq[ch] += 1 without initializing freq[ch] causes a KeyError because the key does not exist.
❓ Predict Output
advanced2:00remaining
Output of character frequency count using Counter
What is the output of this Python code using collections.Counter to count characters in 'banana'?
DSA Python
from collections import Counter text = "banana" freq = Counter(text) print(dict(freq))
Attempts:
2 left
💡 Hint
Count how many times each letter appears in 'banana'.
✗ Incorrect
Counter counts each character's occurrences. 'a' appears 3 times, 'n' 2 times, and 'b' once.
🚀 Application
expert2:30remaining
Find the character with the highest frequency
Given the string 'abracadabra', which character has the highest frequency and what is its count?
DSA Python
text = "abracadabra" freq = {} for ch in text: freq[ch] = freq.get(ch, 0) + 1 max_char = max(freq, key=freq.get) max_count = freq[max_char] print(max_char, max_count)
Attempts:
2 left
💡 Hint
Count how many times each letter appears and find the one with the largest count.
✗ Incorrect
The letter 'a' appears 5 times, which is more than any other character in 'abracadabra'.