Challenge - 5 Problems
Character Frequency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of character frequency count in a string
What is the output of the following TypeScript code that counts character frequency in a string?
DSA Typescript
function charFrequency(str: string): Record<string, number> { const freq: Record<string, number> = {}; for (const ch of str) { freq[ch] = (freq[ch] || 0) + 1; } return freq; } console.log(charFrequency("hello"));
Attempts:
2 left
💡 Hint
Count how many times each letter appears in the string "hello".
✗ Incorrect
The function counts each character's occurrences. In "hello", 'l' appears twice, others once.
❓ Predict Output
intermediateCharacter frequency with spaces included
What is the output of this TypeScript code counting characters including spaces?
DSA Typescript
function charFrequency(str: string): Record<string, number> { const freq: Record<string, number> = {}; for (const ch of str) { freq[ch] = (freq[ch] || 0) + 1; } return freq; } console.log(charFrequency("a b a"));
Attempts:
2 left
💡 Hint
Remember to count spaces as characters too.
✗ Incorrect
The string "a b a" has two 'a's, one 'b', and two spaces.
🔧 Debug
advancedIdentify the error in character frequency code
What error does this TypeScript code produce when counting character frequency?
DSA Typescript
function charFrequency(str: string): Record<string, number> { const freq: Record<string, number> = {}; for (const ch of str) { freq[ch] += 1; } return freq; } console.log(charFrequency("test"));
Attempts:
2 left
💡 Hint
Check what happens when freq[ch] is undefined before adding 1.
✗ Incorrect
The code tries to add 1 to freq[ch] without initializing it first, causing a TypeScript compile error because freq[ch] may be undefined.
🚀 Application
advancedFind the most frequent character
Given the character frequency object, which code snippet correctly finds the character with the highest frequency?
DSA Typescript
const freq = { a: 3, b: 5, c: 2 };
// Find character with max frequencyAttempts:
2 left
💡 Hint
Check which option correctly updates maxChar and maxCount to find max frequency.
✗ Incorrect
Option C correctly iterates and updates maxChar when a higher frequency is found.
🧠 Conceptual
expertTime complexity of character frequency counting
What is the time complexity of counting character frequency in a string of length n using a hash map?
Attempts:
2 left
💡 Hint
Consider how many times the loop runs and the cost of each operation.
✗ Incorrect
Counting frequency requires one pass through the string, so it is O(n).
