Challenge - 5 Problems
Frequency Counter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of frequency count for characters in a string
What is the output of the following Python code that counts character frequencies in a string?
DSA Python
def char_frequency(s): freq = {} for char in s: freq[char] = freq.get(char, 0) + 1 return freq result = char_frequency('banana') print(result)
Attempts:
2 left
💡 Hint
Count how many times each character appears in 'banana'.
✗ Incorrect
The function counts each character's occurrences. 'b' appears once, 'a' appears three times, and 'n' appears twice.
❓ Predict Output
intermediate2:00remaining
Frequency count of numbers in a list
What will be printed after running this code that counts frequencies of numbers in a list?
DSA Python
def count_numbers(nums): freq = {} for num in nums: freq[num] = freq.get(num, 0) + 1 return freq print(count_numbers([1,2,2,3,3,3,4]))
Attempts:
2 left
💡 Hint
Check how many times each number appears in the list.
✗ Incorrect
The list has one 1, two 2s, three 3s, and one 4. The dictionary keys are integers, not strings.
🔧 Debug
advanced2:00remaining
Identify the error in frequency counter code
What error does this code raise when counting frequencies of elements in a list?
DSA Python
def freq_counter(arr): freq = {} for item in arr: freq[item] += 1 return freq print(freq_counter([1,2,2,3]))
Attempts:
2 left
💡 Hint
Consider what happens when you try to add 1 to a key that does not exist yet.
✗ Incorrect
The code tries to increment freq[item] without initializing it first, causing a KeyError.
🚀 Application
advanced3:00remaining
Find if two strings are anagrams using frequency counter
Given two strings, which code correctly uses frequency counters to check if they are anagrams?
Attempts:
2 left
💡 Hint
Check that frequencies match by incrementing for first string and decrementing for second.
✗ Incorrect
Option C correctly increments counts for s1, decrements for s2, and checks all counts are zero, confirming anagrams.
🧠 Conceptual
expert1:30remaining
Time complexity of frequency counter pattern
What is the time complexity of counting frequencies of elements in a list of length n using a hash map?
Attempts:
2 left
💡 Hint
Consider how many times each element is processed and the average time for hash map operations.
✗ Incorrect
Each element is processed once, and hash map insertions and lookups are average O(1), so total is O(n).