Bird
0
0
DSA Cprogramming~20 mins

Hash Table Concept and Hash Functions in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Hash Table Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Simple Hash Function Modulo Operation
What is the output of the following code that simulates a hash function using modulo operation on keys?
DSA C
int keys[] = {10, 22, 31, 4, 15};
int size = 10;
for (int i = 0; i < 5; i++) {
    int hash = keys[i] % size;
    printf("%d -> %d\n", keys[i], hash);
}
A
10 -&gt; 0
22 -&gt; 2
31 -&gt; 3
4 -&gt; 4
15 -&gt; 5
B
10 -&gt; 1
22 -&gt; 2
31 -&gt; 3
4 -&gt; 4
15 -&gt; 5
C
10 -&gt; 0
22 -&gt; 2
31 -&gt; 1
4 -&gt; 4
15 -&gt; 5
D
10 -&gt; 0
22 -&gt; 1
31 -&gt; 2
4 -&gt; 3
15 -&gt; 4
Attempts:
2 left
💡 Hint
Remember that modulo operation returns the remainder after division.
🧠 Conceptual
intermediate
1:30remaining
Understanding Hash Collisions
Which of the following statements best describes a hash collision in a hash table?
AA key is not found in the hash table.
BTwo different keys produce the same hash value.
CThe hash function returns a negative number.
DThe hash table is empty.
Attempts:
2 left
💡 Hint
Think about what happens when two keys map to the same slot.
Predict Output
advanced
2:30remaining
Output of Hash Function with String Keys
What is the output of this code that calculates a simple hash for strings by summing ASCII values and taking modulo?
DSA C
char* keys[] = {"abc", "bcd", "ace"};
int size = 5;
for (int i = 0; i < 3; i++) {
    int sum = 0;
    for (int j = 0; keys[i][j] != '\0'; j++) {
        sum += keys[i][j];
    }
    int hash = sum % size;
    printf("%s -> %d\n", keys[i], hash);
}
A
abc -&gt; 1
bcd -&gt; 3
ace -&gt; 0
B
abc -&gt; 3
bcd -&gt; 0
ace -&gt; 1
C
abc -&gt; 4
bcd -&gt; 2
ace -&gt; 2
D
abc -&gt; 2
bcd -&gt; 4
ace -&gt; 1
Attempts:
2 left
💡 Hint
Calculate ASCII sums carefully, then apply modulo 5.
🔧 Debug
advanced
2:00remaining
Identify the Error in Hash Function Implementation
What error does the following hash function code produce when compiling or running?
DSA C
int hash(char* key, int size) {
    int sum = 0;
    for (int i = 0; key[i] != '\0'; i++)
        sum += key[i];
    return sum / size;
}

int main() {
    char* key = "test";
    int size = 10;
    int h = hash(key, size);
    printf("Hash: %d\n", h);
    return 0;
}
ANo error; outputs hash value.
BRuntime error: division by zero if size is zero.
CSyntax error: missing braces in for loop.
DLogical error: uses division instead of modulo, wrong hash output.
Attempts:
2 left
💡 Hint
Check the arithmetic operation used to reduce the sum to a hash index.
🧠 Conceptual
expert
1:30remaining
Choosing a Good Hash Function Property
Which property is most important for a hash function to minimize collisions in a hash table?
AUniform distribution of hash values across the table.
BFast computation time regardless of input size.
CDeterministic output for the same input.
DAbility to handle only numeric keys.
Attempts:
2 left
💡 Hint
Think about how keys spread in the hash table.