0
0
DSA Pythonprogramming~20 mins

Frequency Counter Pattern Using Hash Map in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Frequency Counter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A{'b': 2, 'a': 3, 'n': 2}
B{'b': 1, 'a': 2, 'n': 3}
C{'b': 1, 'a': 3, 'n': 3}
D{'b': 1, 'a': 3, 'n': 2}
Attempts:
2 left
💡 Hint
Count how many times each character appears in 'banana'.
Predict Output
intermediate
2: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]))
A{1: 1, 2: 2, 3: 2, 4: 1}
B{1: 1, 2: 2, 3: 3, 4: 1}
C{1: 1, 2: 3, 3: 2, 4: 1}
D{"1": 1, "2": 2, "3": 3, "4": 1}
Attempts:
2 left
💡 Hint
Check how many times each number appears in the list.
🔧 Debug
advanced
2: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]))
AKeyError
BTypeError
CSyntaxError
DNo error, outputs {1:1, 2:2, 3:1}
Attempts:
2 left
💡 Hint
Consider what happens when you try to add 1 to a key that does not exist yet.
🚀 Application
advanced
3: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?
A
def are_anagrams(s1, s2):
    if len(s1) != len(s2):
        return False
    freq1 = {}
    freq2 = {}
    for c in s1:
        freq1[c] = freq1.get(c, 0) + 1
    for c in s2:
        freq2[c] = freq2.get(c, 0) + 1
    return freq1 == freq2
B
def are_anagrams(s1, s2):
    return sorted(s1) == sorted(s2)
C
def are_anagrams(s1, s2):
    freq = {}
    for c in s1:
        freq[c] = freq.get(c, 0) + 1
    for c in s2:
        if c not in freq:
            return False
        freq[c] -= 1
    return all(value == 0 for value in freq.values())
D
def are_anagrams(s1, s2):
    freq = {}
    for c in s1:
        freq[c] = freq.get(c, 0) + 1
    for c in s2:
        freq[c] = freq.get(c, 0) - 1
    return all(value == 0 for value in freq.values())
Attempts:
2 left
💡 Hint
Check that frequencies match by incrementing for first string and decrementing for second.
🧠 Conceptual
expert
1: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?
AO(n)
BO(n log n)
CO(n^2)
DO(log n)
Attempts:
2 left
💡 Hint
Consider how many times each element is processed and the average time for hash map operations.