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 TypeScript code that counts character frequencies in a string?
DSA Typescript
function charFrequency(str: string): Record<string, number> { const freq: Record<string, number> = {}; for (const char of str) { freq[char] = (freq[char] || 0) + 1; } return freq; } console.log(charFrequency("hello"));
Attempts:
2 left
💡 Hint
Count how many times each letter appears in the word "hello".
✗ Incorrect
The function counts each character's occurrences. 'l' appears twice, others once.
❓ Predict Output
intermediateFrequency Counter for Numbers in Array
What will be printed after running this TypeScript code that counts frequencies of numbers in an array?
DSA Typescript
function numFrequency(arr: number[]): Record<number, number> {
const freq: Record<number, number> = {};
for (const num of arr) {
freq[num] = (freq[num] || 0) + 1;
}
return freq;
}
console.log(numFrequency([1,2,2,3,3,3]));Attempts:
2 left
💡 Hint
Count how many times each number appears in the array.
✗ Incorrect
Number 1 appears once, 2 appears twice, and 3 appears three times.
🧠 Conceptual
advancedWhy Use Frequency Counter Pattern?
Which of the following best explains why the frequency counter pattern using a hash map is efficient for comparing two arrays for the same frequency of elements?
Attempts:
2 left
💡 Hint
Think about how hash maps store counts and how that helps avoid repeated scanning.
✗ Incorrect
Frequency counters store counts in a hash map, enabling O(n) time comparison without nested loops.
🔧 Debug
advancedIdentify the Bug in Frequency Counter Code
What will this TypeScript code output when counting character frequencies?
DSA Typescript
function freqCounter(str: string): Record<string, number> { const freq: Record<string, number> = {}; for (let i = 0; i < str.length; i++) { const char = str[i]; freq[char] = (freq[char] || 0) + 1; } return freq; } console.log(freqCounter("abc"));
Attempts:
2 left
💡 Hint
Check the loop boundary and what happens when index equals string length.
✗ Incorrect
The loop boundary was off-by-one, causing an extra count for 'undefined'. Fixed to i < str.length to avoid this.
🚀 Application
expertFrequency Counter to Check Anagrams
Given two strings, which option correctly uses frequency counters to check if they are anagrams (same letters with same counts)?
DSA Typescript
function areAnagrams(str1: string, str2: string): boolean {
if (str1.length !== str2.length) return false;
const freq1: Record<string, number> = {};
const freq2: Record<string, number> = {};
for (const char of str1) {
freq1[char] = (freq1[char] || 0) + 1;
}
for (const char of str2) {
freq2[char] = (freq2[char] || 0) + 1;
}
for (const key in freq1) {
if (freq1[key] !== freq2[key]) return false;
}
return true;
}
console.log(areAnagrams("listen", "silent"));Attempts:
2 left
💡 Hint
Anagrams have the same frequency of each character.
✗ Incorrect
The function counts characters in both strings and compares counts; 'listen' and 'silent' match.
