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 in a chained hash table
Consider a hash table with 5 buckets using chaining for collision handling. The hash function is
key % 5. Insert keys 7, 12, 17, 22 in this order. What is the printed state of the hash table after all insertions?DSA Java
class Node { int key; Node next; Node(int key) { this.key = key; this.next = null; } } class HashTable { Node[] table = new Node[5]; void insert(int key) { int index = key % 5; Node newNode = new Node(key); if (table[index] == null) { table[index] = newNode; } else { newNode.next = table[index]; table[index] = newNode; } } void printTable() { for (int i = 0; i < table.length; i++) { System.out.print(i + ": "); Node current = table[i]; while (current != null) { System.out.print(current.key + " -> "); current = current.next; } System.out.println("null"); } } } public class Main { public static void main(String[] args) { HashTable ht = new HashTable(); ht.insert(7); ht.insert(12); ht.insert(17); ht.insert(22); ht.printTable(); } }
Attempts:
2 left
💡 Hint
Remember the hash function is key % 5, and new nodes are inserted at the head of the chain.
✗ Incorrect
All keys 7, 12, 17, and 22 have hash index 2 (since 7%5=2, 12%5=2, etc.). Each new key is inserted at the head of the linked list at index 2, so the order is last inserted first: 22 -> 17 -> 12 -> 7 -> null.
🧠 Conceptual
intermediateNumber of elements in a chained bucket after insertions
A hash table has 7 buckets and uses chaining. The hash function is
key % 7. If keys 14, 21, 28, 35, 42 are inserted, how many elements will be in the bucket at index 0?Attempts:
2 left
💡 Hint
Calculate the hash index for each key using key % 7.
✗ Incorrect
All keys are multiples of 7, so key % 7 = 0 for all. Therefore, all 5 keys go into bucket 0.
🔧 Debug
advancedIdentify the error in chaining insertion code
What error will occur when running this chaining insertion code snippet?
DSA Java
class Node { int key; Node next; Node(int key) { this.key = key; this.next = null; } } class HashTable { Node[] table = new Node[3]; void insert(int key) { int index = key % 3; Node newNode = new Node(key); if (table[index] != null) { newNode.next = table[index]; table[index] = newNode; } else { table[index] = newNode; } } } public class Main { public static void main(String[] args) { HashTable ht = new HashTable(); ht.insert(1); ht.insert(4); ht.insert(7); System.out.println("Insertion done"); } }
Attempts:
2 left
💡 Hint
Check the insert method logic and array initialization.
✗ Incorrect
The insert method correctly handles null and non-null buckets. The array size is 3, and keys 1,4,7 hash to indices 1,1,1 respectively, all valid. No exceptions occur.
❓ Predict Output
advancedOutput after deleting a key from chained hash table
Given a chained hash table with 3 buckets and hash function
key % 3, keys 3, 6, 9 are inserted. Then key 6 is deleted. What is the printed state of the hash table?DSA Java
class Node { int key; Node next; Node(int key) { this.key = key; this.next = null; } } class HashTable { Node[] table = new Node[3]; void insert(int key) { int index = key % 3; Node newNode = new Node(key); newNode.next = table[index]; table[index] = newNode; } void delete(int key) { int index = key % 3; Node current = table[index]; Node prev = null; while (current != null) { if (current.key == key) { if (prev == null) { table[index] = current.next; } else { prev.next = current.next; } return; } prev = current; current = current.next; } } void printTable() { for (int i = 0; i < table.length; i++) { System.out.print(i + ": "); Node current = table[i]; while (current != null) { System.out.print(current.key + " -> "); current = current.next; } System.out.println("null"); } } } public class Main { public static void main(String[] args) { HashTable ht = new HashTable(); ht.insert(3); ht.insert(6); ht.insert(9); ht.delete(6); ht.printTable(); } }
Attempts:
2 left
💡 Hint
Remember insertion adds new nodes at the head. Deletion removes the node with key 6.
✗ Incorrect
Keys 3,6,9 all hash to index 0 (3%3=0, 6%3=0, 9%3=0). Insertions add at head, so list is 9 -> 6 -> 3 -> null. Deleting 6 removes the middle node, resulting in 9 -> 3 -> null at index 0. The printed output shows index 0 with 9 -> 3 -> null, others null.
🧠 Conceptual
expertLoad factor impact on chaining performance
In a hash table using chaining, if the load factor (number of elements / number of buckets) increases significantly, what is the most likely effect on the average time complexity for search operations?
Attempts:
2 left
💡 Hint
Think about how longer chains affect search time.
✗ Incorrect
As load factor increases, chains get longer, so searching requires traversing more nodes, making average search time approach O(n) where n is number of elements.
