Bird
0
0
DSA Cprogramming~20 mins

Character Frequency Counting in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Character Frequency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of character frequency count 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[] = "hello world";
    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;
}
Ah:1 e:1 l:3 o:1 :1 w:1 r:1 d:1
Bh:1 e:1 l:2 o:2 :1 w:1 r:1 d:1
Ch:1 e:1 l:3 o:2 :1 w:1 r:1 d:1
Dh:1 e:1 l:3 o:2 :2 w:1 r:1 d:1
Attempts:
2 left
💡 Hint
Count each character's occurrences carefully, including spaces.
🧠 Conceptual
intermediate
1:30remaining
Understanding character frequency array size
Why is the frequency array usually declared with size 256 when counting characters in C?
ABecause it is the size of the string being processed.
BBecause 256 is the maximum number of characters in any string.
CBecause ASCII characters fit in 256 values, covering all possible characters in extended ASCII.
DBecause 256 is the number of letters in the English alphabet.
Attempts:
2 left
💡 Hint
Think about the range of unsigned char values.
🔧 Debug
advanced
2:00remaining
Identify the error in character frequency counting code
What error will the following code produce when counting character frequencies?
DSA C
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "abcabc";
    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;
}
AThe code will print frequencies but with wrong characters due to casting.
BThe code will cause a runtime error due to uninitialized freq array values.
CThe code will cause a compilation error because freq array size is too small.
DThe code will print correct frequencies without error.
Attempts:
2 left
💡 Hint
Check if freq array elements are initialized before incrementing.
Predict Output
advanced
2:00remaining
Output of frequency count with non-alphabet characters
What is the output of this C code counting frequencies including digits and punctuation?
DSA C
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "a1!a2!";
    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;
}
A!:2 1:1 2:1 a:2
Ba:2 !:2 1:1 2:1
Ca:2 1:1 2:1 !:1
D1:1 2:1 a:2 !:2
Attempts:
2 left
💡 Hint
Check the order of characters printed by increasing ASCII code.
🧠 Conceptual
expert
2:30remaining
Memory optimization for character frequency counting
Which approach best reduces memory usage when counting frequencies of only lowercase English letters in a large text?
AUse an int array of size 26, indexing by (character - 'a').
BUse an int array of size 256 to cover all ASCII characters.
CUse a hash map with characters as keys and counts as values.
DUse a linked list to store characters and their counts dynamically.
Attempts:
2 left
💡 Hint
Focus on the smallest fixed-size array that covers only needed characters.