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 C code that counts character frequencies in a string?
DSA C
#include <stdio.h> #include <string.h> int main() { char str[] = "banana"; int freq[256] = {0}; for (int i = 0; i < strlen(str); i++) { freq[(unsigned char)str[i]]++; } for (int i = 0; i < 256; i++) { if (freq[i] > 0) { printf("%c:%d ", i, freq[i]); } } return 0; }
Attempts:
2 left
💡 Hint
Count how many times each character appears in the string "banana".
✗ Incorrect
The string "banana" has 'b' once, 'a' three times, and 'n' twice. The code prints characters in ASCII order, so 'a' comes before 'b' and 'n'.
❓ Predict Output
intermediate2:00remaining
Frequency count of integers in an array
What is the output of this C code that counts frequency of integers in an array?
DSA C
#include <stdio.h> int main() { int arr[] = {2, 3, 2, 4, 3, 2}; int freq[5] = {0}; int n = sizeof(arr) / sizeof(arr[0]); for (int i = 0; i < n; i++) { freq[arr[i]]++; } for (int i = 0; i < 5; i++) { if (freq[i] > 0) { printf("%d:%d ", i, freq[i]); } } return 0; }
Attempts:
2 left
💡 Hint
Count how many times each number appears in the array.
✗ Incorrect
The array has 2 three times, 3 two times, and 4 once. The code prints frequencies for numbers 0 to 4.
🔧 Debug
advanced2:00remaining
Identify the error in frequency counting code
What error does this C code produce when counting character frequencies?
DSA C
#include <stdio.h> #include <string.h> int main() { char str[] = "apple"; int freq[128]; for (int i = 0; i < strlen(str); i++) { freq[(int)str[i]]++; } for (int i = 0; i < 128; i++) { if (freq[i] > 0) { printf("%c:%d ", i, freq[i]); } } return 0; }
Attempts:
2 left
💡 Hint
Check if freq array is initialized before incrementing.
✗ Incorrect
The freq array is declared but not initialized to zero, so incrementing freq[i] uses garbage values causing undefined behavior.
🧠 Conceptual
advanced2:00remaining
Frequency counter pattern with non-continuous keys
Which data structure is best to count frequencies of arbitrary integers (not limited range) in C?
Attempts:
2 left
💡 Hint
Think about how to efficiently store counts for any integer key.
✗ Incorrect
A hash map allows mapping any integer key to its frequency efficiently without large memory waste.
❓ Predict Output
expert3:00remaining
Output of frequency count with mixed characters
What is the output of this C code that counts frequencies of letters and digits in a string?
DSA C
#include <stdio.h> #include <string.h> int main() { char str[] = "a1b2a1"; int freq[256] = {0}; for (int i = 0; i < strlen(str); i++) { freq[(unsigned char)str[i]]++; } for (int i = 48; i <= 57; i++) { // digits 0-9 if (freq[i] > 0) { printf("%c:%d ", i, freq[i]); } } for (int i = 97; i <= 122; i++) { // lowercase letters a-z if (freq[i] > 0) { printf("%c:%d ", i, freq[i]); } } return 0; }
Attempts:
2 left
💡 Hint
Count digits and letters separately and print in order.
✗ Incorrect
The string "a1b2a1" has '1' twice, '2' once, 'a' twice, and 'b' once. The code prints digits first then letters.
