Challenge - 5 Problems
Hash Table Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); }
Attempts:
2 left
💡 Hint
Remember that modulo operation returns the remainder after division.
✗ Incorrect
Each key is divided by the size (10), and the remainder is the hash value. For example, 10 % 10 = 0, 22 % 10 = 2, etc.
🧠 Conceptual
intermediate1:30remaining
Understanding Hash Collisions
Which of the following statements best describes a hash collision in a hash table?
Attempts:
2 left
💡 Hint
Think about what happens when two keys map to the same slot.
✗ Incorrect
A hash collision occurs when two distinct keys generate the same hash index, causing them to compete for the same position in the table.
❓ Predict Output
advanced2: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);
}Attempts:
2 left
💡 Hint
Calculate ASCII sums carefully, then apply modulo 5.
✗ Incorrect
Sum of ASCII values modulo table size (5):
'abc' (97+98+99=294) % 5 = 4
'bcd' (98+99+100=297) % 5 = 2
'ace' (97+99+101=297) % 5 = 2
🔧 Debug
advanced2: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; }
Attempts:
2 left
💡 Hint
Check the arithmetic operation used to reduce the sum to a hash index.
✗ Incorrect
The function uses division (sum / size) instead of modulo (sum % size), so the hash value is incorrect and may exceed table size.
🧠 Conceptual
expert1:30remaining
Choosing a Good Hash Function Property
Which property is most important for a hash function to minimize collisions in a hash table?
Attempts:
2 left
💡 Hint
Think about how keys spread in the hash table.
✗ Incorrect
Uniform distribution ensures keys spread evenly, reducing collisions and improving performance.
