Challenge - 5 Problems
Chaining Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of inserting keys with collisions in chaining hash table
What is the printed state of the hash table buckets after inserting keys 5, 15, 25 into a hash table of size 10 using chaining with hash function key % 10?
DSA C#
class Node { public int Key; public Node Next; public Node(int key) { Key = key; Next = null; } } class HashTable { private Node[] buckets = new Node[10]; private int Hash(int key) => key % 10; public void Insert(int key) { int index = Hash(key); Node newNode = new Node(key); if (buckets[index] == null) { buckets[index] = newNode; } else { Node current = buckets[index]; while (current.Next != null) { current = current.Next; } current.Next = newNode; } } public void Print() { for (int i = 0; i < buckets.Length; i++) { Console.Write(i + ": "); Node current = buckets[i]; while (current != null) { Console.Write(current.Key + " -> "); current = current.Next; } Console.WriteLine("null"); } } } var ht = new HashTable(); ht.Insert(5); ht.Insert(15); ht.Insert(25); ht.Print();
Attempts:
2 left
💡 Hint
Remember that chaining adds new nodes at the end of the linked list in the bucket.
✗ Incorrect
The hash function key % 10 maps 5, 15, and 25 all to bucket 5. They are inserted in order, so the linked list at bucket 5 is 5 -> 15 -> 25 -> null.
🧠 Conceptual
intermediateNumber of items in a bucket after collisions
If a hash table uses chaining and the hash function maps keys 12, 22, 32, and 42 to the same bucket, how many nodes will be in that bucket's linked list after inserting all keys?
Attempts:
2 left
💡 Hint
Each key that hashes to the same bucket adds one node to the linked list.
✗ Incorrect
All four keys map to the same bucket, so the linked list in that bucket will have four nodes, one for each key.
🔧 Debug
advancedIdentify the bug in chaining insertion code
What error will occur when running this chaining insertion code snippet?
DSA C#
public void Insert(int key) { int index = key % buckets.Length; Node newNode = new Node(key); if (buckets[index] == null) { buckets[index] = newNode; } else { Node current = buckets[index]; while (current.Next != null) { current = current.Next; } current.Next = newNode; } }
Attempts:
2 left
💡 Hint
Check the while loop condition and what happens when current becomes null.
✗ Incorrect
The while loop continues until current is null, so after the loop current is null. Trying to access current.Next causes NullReferenceException.
❓ Predict Output
advancedState of hash table after deletions in chaining
Given a hash table with chaining and keys 7, 17, 27 inserted (all hash to bucket 7), what is the bucket 7 state after deleting key 17?
DSA C#
class Node { public int Key; public Node Next; public Node(int key) { Key = key; Next = null; } } class HashTable { private Node[] buckets = new Node[10]; private int Hash(int key) => key % 10; public void Insert(int key) { int index = Hash(key); Node newNode = new Node(key); if (buckets[index] == null) { buckets[index] = newNode; } else { Node current = buckets[index]; while (current.Next != null) { current = current.Next; } current.Next = newNode; } } public void Delete(int key) { int index = Hash(key); Node current = buckets[index]; Node prev = null; while (current != null) { if (current.Key == key) { if (prev == null) { buckets[index] = current.Next; } else { prev.Next = current.Next; } return; } prev = current; current = current.Next; } } public void PrintBucket(int index) { Console.Write(index + ": "); Node current = buckets[index]; while (current != null) { Console.Write(current.Key + " -> "); current = current.Next; } Console.WriteLine("null"); } } var ht = new HashTable(); ht.Insert(7); ht.Insert(17); ht.Insert(27); ht.Delete(17); ht.PrintBucket(7);
Attempts:
2 left
💡 Hint
Deleting key 17 removes it from the linked list, linking 7 directly to 27.
✗ Incorrect
After deleting 17, the linked list in bucket 7 contains 7 and 27 linked directly.
🧠 Conceptual
expertEffect of poor hash function on chaining performance
What is the main effect on a hash table using chaining if the hash function maps most keys to the same bucket?
Attempts:
2 left
💡 Hint
Think about what happens when many keys collide into one bucket.
✗ Incorrect
If many keys map to the same bucket, the linked list grows long, making search, insert, and delete operations slower, similar to a linked list.
