Challenge - 5 Problems
Frequency Counter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Frequency Counter for Characters
What is the output of the following JavaScript code that counts character frequencies in a string?
DSA Javascript
const str = "banana"; const freq = {}; for (const char of str) { freq[char] = (freq[char] || 0) + 1; } console.log(freq);
Attempts:
2 left
💡 Hint
Count how many times each letter appears in "banana".
✗ Incorrect
The string "banana" has 1 'b', 3 'a's, and 2 'n's. The code counts each character's occurrences correctly.
❓ Predict Output
intermediateFrequency Counter for Array Elements
What will be printed after running this JavaScript code that counts frequencies of numbers in an array?
DSA Javascript
const arr = [2, 3, 2, 4, 3, 2]; const freq = {}; for (const num of arr) { freq[num] = (freq[num] || 0) + 1; } console.log(freq);
Attempts:
2 left
💡 Hint
Count how many times each number appears in the array.
✗ Incorrect
Number 2 appears 3 times, 3 appears 2 times, and 4 appears once.
🧠 Conceptual
advancedFrequency Counter Pattern Use Case
Which of the following problems is best solved using the frequency counter pattern with a hash map?
Attempts:
2 left
💡 Hint
Think about comparing character counts between two strings.
✗ Incorrect
Anagrams have the same frequency of each character, so counting frequencies helps verify this efficiently.
🔧 Debug
advancedIdentify the Bug in Frequency Counter Code
What error or issue will occur when running this code snippet?
DSA Javascript
const arr = [1, 2, 2, 3]; const freq = new Map(); for (const num of arr) { if (freq.has(num)) { freq.set(num, freq.get(num) + 1); } else { freq[num] = 1; } } console.log(freq);
Attempts:
2 left
💡 Hint
Check how Map objects store values and how to add new keys.
✗ Incorrect
Using freq[num] = 1 adds a property to the object but does not update the Map. The Map remains missing that key.
❓ Predict Output
expertFrequency Counter with Nested Objects Output
What is the output of this JavaScript code that counts frequencies of words and their first letters?
DSA Javascript
const words = ["apple", "apricot", "banana", "blueberry", "avocado"]; const freq = {}; for (const word of words) { const first = word[0]; freq[first] = freq[first] || {}; freq[first][word] = (freq[first][word] || 0) + 1; } console.log(freq);
Attempts:
2 left
💡 Hint
Group words by their first letter and count each word's frequency.
✗ Incorrect
Words starting with 'a' are apple, apricot, avocado each counted once; words starting with 'b' are banana and blueberry each once.
