Challenge - 5 Problems
Hash Table Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this hash function simulation?
Consider a simple hash function that calculates the sum of ASCII values of characters in a string and then takes modulo 10. What is the output index for the key "cat"?
DSA C#
string key = "cat"; int hash = 0; foreach (char c in key) { hash += (int)c; } int index = hash % 10; Console.WriteLine(index);
Attempts:
2 left
💡 Hint
Calculate ASCII values: 'c' = 99, 'a' = 97, 't' = 116.
✗ Incorrect
Sum of ASCII values: 'c' (99) + 'a' (97) + 't' (116) = 312. Then 312 % 10 = 2. The output is 2.
❓ Predict Output
intermediateWhat is the output of this hash table insertion simulation?
Given a hash table of size 5 and a hash function h(key) = key % 5, what is the final state of the table after inserting keys 7, 12, 17 using linear probing for collision resolution?
DSA C#
int size = 5; int[] table = new int[size]; for (int i = 0; i < size; i++) table[i] = -1; int[] keys = {7, 12, 17}; foreach (int key in keys) { int index = key % size; while (table[index] != -1) { index = (index + 1) % size; } table[index] = key; } Console.WriteLine("[" + string.Join(", ", table) + "]");
Attempts:
2 left
💡 Hint
Calculate initial indices and resolve collisions by moving to next empty slot.
✗ Incorrect
7 % 5 = 2, insert at index 2.
12 % 5 = 2 (collision), probe to index 3.
17 % 5 = 2 (collisions at 2 and 3), probe to index 4.
Final table: [-1, -1, 7, 12, 17].
🔧 Debug
advancedWhat error does this hash function code raise?
Identify the error raised by this C# hash function code snippet.
DSA C#
string key = null; int hash = 0; foreach (char c in key) { hash += (int)c; } int index = hash % 10; Console.WriteLine(index);
Attempts:
2 left
💡 Hint
What happens when you try to loop over a null string?
✗ Incorrect
Since 'key' is null, trying to iterate over it causes a NullReferenceException.
🧠 Conceptual
advancedWhich property is NOT essential for a good hash function?
Select the property that is NOT essential for a good hash function used in hash tables.
Attempts:
2 left
💡 Hint
Think about what a hash function is used for in hash tables.
✗ Incorrect
A good hash function must be deterministic, fast, and distribute keys uniformly.
Reversing the hash is not required and usually impossible.
🚀 Application
expertWhat is the final linked list at bucket 3 after these insertions?
A hash table uses chaining with linked lists for collisions. The hash function is h(key) = key % 4. Insert keys 3, 7, 11, 15 in order. What is the linked list at bucket 3 after all insertions?
DSA C#
int size = 4; List<int>[] table = new List<int>[size]; for (int i = 0; i < size; i++) table[i] = new List<int>(); int[] keys = {3, 7, 11, 15}; foreach (int key in keys) { int index = key % size; table[index].Add(key); } Console.WriteLine(string.Join(" -> ", table[3]) + " -> null");
Attempts:
2 left
💡 Hint
Calculate key % 4 for each key and add to corresponding bucket list in order.
✗ Incorrect
All keys 3,7,11,15 % 4 = 3, so all go to bucket 3.
They are inserted in order: 3, then 7, then 11, then 15.
Final linked list: 3 -> 7 -> 11 -> 15 -> null.
