Bird
0
0
DSA Cprogramming~20 mins

Frequency Counter Pattern Using Hash Map in DSA C - 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 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;
}
Ab:1 a:2 n:3
Bb:1 a:3 n:2
Ca:2 b:1 n:3
Da:3 b:1 n:2
Attempts:
2 left
💡 Hint
Count how many times each character appears in the string "banana".
Predict Output
intermediate
2: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;
}
A2:3 3:1 4:2
B2:2 3:3 4:1
C2:3 3:2 4:1
D2:1 3:2 4:3
Attempts:
2 left
💡 Hint
Count how many times each number appears in the array.
🔧 Debug
advanced
2: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;
}
AUses uninitialized values in freq array causing undefined behavior
BSyntax error due to missing semicolon
CArray index out of bounds error
DNo error, prints correct frequencies
Attempts:
2 left
💡 Hint
Check if freq array is initialized before incrementing.
🧠 Conceptual
advanced
2:00remaining
Frequency counter pattern with non-continuous keys
Which data structure is best to count frequencies of arbitrary integers (not limited range) in C?
AUse a fixed-size array indexed by integer values
BUse a hash map (hash table) to map integers to counts
CUse a stack to push integers and count later
DUse a linked list to store pairs of integer and count
Attempts:
2 left
💡 Hint
Think about how to efficiently store counts for any integer key.
Predict Output
expert
3: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;
}
A1:2 2:1 a:2 b:1
B1:1 2:1 a:2 b:1
C1:2 2:1 a:1 b:1
D1:2 2:2 a:2 b:1
Attempts:
2 left
💡 Hint
Count digits and letters separately and print in order.