Bird
Raised Fist0
DSA Cprogramming~20 mins

Frequency Counter Pattern Using Hash Map in DSA C - Practice Problems & Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using a hash map in the Frequency Counter Pattern?
easy
A. To reverse the order of elements in the collection
B. To sort the elements in ascending order
C. To remove duplicate elements from the collection
D. To count how many times each element appears in a collection

Solution

  1. Step 1: Understand the role of a hash map

    A hash map stores key-value pairs, allowing quick lookup and update of counts.
  2. Step 2: Apply to frequency counting

    Using a hash map, we can track how many times each element appears by incrementing counts.
  3. Final Answer:

    To count how many times each element appears in a collection -> Option D
  4. Quick Check:

    Frequency counting = Counting elements [OK]
Hint: Hash map keys track elements, values track counts [OK]
Common Mistakes:
  • Thinking hash map sorts elements
  • Confusing frequency counting with removing duplicates
  • Using arrays instead of hash maps for counting
2. Which of the following is the correct syntax to declare a hash map in C for counting frequencies of characters?
easy
A. map freq;
B. int freq[256] = {0};
C. unordered_map freq;
D. HashMap freq = new HashMap();

Solution

  1. Step 1: Recognize C language limitations

    C does not have built-in hash map types like C++ STL's map or unordered_map.
  2. Step 2: Use array for frequency counting in C

    We use an integer array of size 256 (for ASCII chars) initialized to zero to count frequencies.
  3. Final Answer:

    int freq[256] = {0}; -> Option B
  4. Quick Check:

    C uses arrays for frequency counting [OK]
Hint: In C, use arrays for frequency counts, not map types [OK]
Common Mistakes:
  • Using C++ STL syntax in C code
  • Trying to declare map or unordered_map in C
  • Not initializing frequency array to zero
3. What will be the output of the following C code snippet?
char str[] = "aabbcc";
int freq[256] = {0};
for (int i = 0; str[i] != '\0'; i++) {
    freq[(int)str[i]]++;
}
printf("%d", freq['b']);
medium
A. 0
B. 1
C. 2
D. 3

Solution

  1. Step 1: Count occurrences of 'b' in the string

    The string "aabbcc" has two 'b' characters at positions 2 and 3.
  2. Step 2: Check frequency array value for 'b'

    freq['b'] is incremented twice, so freq['b'] = 2.
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    Count of 'b' = 2 [OK]
Hint: Count characters manually, then check freq array [OK]
Common Mistakes:
  • Confusing character ASCII codes
  • Forgetting null terminator in loop condition
  • Printing freq for wrong character
4. Identify the error in the following C code that tries to count character frequencies:
char str[] = "hello";
int freq[256];
for (int i = 0; str[i] != '\0'; i++) {
    freq[(int)str[i]]++;
}
printf("%d", freq['l']);
medium
A. freq array is not initialized to zero
B. Loop condition should be i < strlen(str)
C. Casting str[i] to int is incorrect
D. printf format specifier should be %c

Solution

  1. Step 1: Check initialization of freq array

    The freq array is declared but not initialized, so it contains garbage values.
  2. Step 2: Effect on frequency counting

    Incrementing uninitialized values leads to incorrect counts; freq should be zeroed before use.
  3. Final Answer:

    freq array is not initialized to zero -> Option A
  4. Quick Check:

    Initialize freq array to zero before counting [OK]
Hint: Always zero frequency array before counting [OK]
Common Mistakes:
  • Assuming uninitialized arrays start at zero
  • Using wrong printf format for integers
  • Misunderstanding loop conditions with null terminator
5. Given two strings s1 = "listen" and s2 = "silent", which approach using frequency counters correctly checks if they are anagrams in C?
hard
A. Count frequencies of characters in both strings using arrays, then compare arrays element-wise
B. Sort both strings and compare them character by character
C. Check if lengths are equal, then check if first characters match
D. Use a hash map to store characters of s1 only and check if s2 contains them

Solution

  1. Step 1: Use frequency arrays for both strings

    Create two arrays freq1 and freq2 of size 256, initialize to zero, count characters in s1 and s2 respectively.
  2. Step 2: Compare frequency arrays element-wise

    If all counts match for every character, s1 and s2 are anagrams; otherwise, they are not.
  3. Final Answer:

    Count frequencies of characters in both strings using arrays, then compare arrays element-wise -> Option A
  4. Quick Check:

    Frequency arrays equal means anagrams [OK]
Hint: Compare frequency arrays to confirm anagrams [OK]
Common Mistakes:
  • Only counting one string's characters
  • Comparing only string lengths or first characters
  • Assuming sorting is the only way to check anagrams
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard