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 1, 11, and 21 into a hash table of size 10 using chaining with hash function key % 10?
DSA Go
package main import ( "fmt" ) type Node struct { key int next *Node } type HashTable struct { size int buckets []*Node } func NewHashTable(size int) *HashTable { return &HashTable{ size: size, buckets: make([]*Node, size), } } func (ht *HashTable) hash(key int) int { return key % ht.size } func (ht *HashTable) Insert(key int) { index := ht.hash(key) newNode := &Node{key: key} if ht.buckets[index] == nil { ht.buckets[index] = newNode } else { current := ht.buckets[index] for current.next != nil { current = current.next } current.next = newNode } } func (ht *HashTable) Print() { for i, node := range ht.buckets { fmt.Printf("Bucket %d: ", i) current := node for current != nil { fmt.Printf("%d -> ", current.key) current = current.next } fmt.Println("null") } } func main() { ht := NewHashTable(10) ht.Insert(1) ht.Insert(11) ht.Insert(21) ht.Print() }
Attempts:
2 left
💡 Hint
Remember the hash function is key % 10 and new keys are added at the end of the linked list in the bucket.
✗ Incorrect
The hash function maps keys 1, 11, and 21 to bucket 1 (since 1 % 10 = 1, 11 % 10 = 1, 21 % 10 = 1). They are inserted in order at the end of the linked list, so the bucket 1 chain is 1 -> 11 -> 21 -> null.
🧠 Conceptual
intermediateUnderstanding collision handling with chaining
Why does chaining handle collisions effectively in a hash table?
Attempts:
2 left
💡 Hint
Think about how multiple keys can be stored in the same bucket without losing data.
✗ Incorrect
Chaining stores collided keys in a linked list at the bucket index, so all keys with the same hash can be stored without overwriting each other.
🔧 Debug
advancedFind the bug in chaining insertion code
What error will occur when running this chaining insertion code snippet?
DSA Go
func (ht *HashTable) Insert(key int) { index := ht.hash(key) newNode := &Node{key: key} if ht.buckets[index] == nil { ht.buckets[index] = newNode } else { current := ht.buckets[index] for current.next != nil { current = current.next } current = newNode } }
Attempts:
2 left
💡 Hint
Check how the new node is linked to the existing list in the else block.
✗ Incorrect
The line 'current = newNode' only changes the local variable 'current' and does not link the new node to the list. The correct way is 'current.next = newNode'.
❓ Predict Output
advancedOutput after deleting a key from chaining hash table
What is the printed state of the hash table buckets after inserting keys 2, 12, 22 and then deleting key 12 from a hash table of size 10 using chaining with hash function key % 10?
DSA Go
package main import ( "fmt" ) type Node struct { key int next *Node } type HashTable struct { size int buckets []*Node } func NewHashTable(size int) *HashTable { return &HashTable{ size: size, buckets: make([]*Node, size), } } func (ht *HashTable) hash(key int) int { return key % ht.size } func (ht *HashTable) Insert(key int) { index := ht.hash(key) newNode := &Node{key: key} if ht.buckets[index] == nil { ht.buckets[index] = newNode } else { current := ht.buckets[index] for current.next != nil { current = current.next } current.next = newNode } } func (ht *HashTable) Delete(key int) { index := ht.hash(key) current := ht.buckets[index] var prev *Node = nil for current != nil { if current.key == key { if prev == nil { ht.buckets[index] = current.next } else { prev.next = current.next } return } prev = current current = current.next } } func (ht *HashTable) Print() { for i, node := range ht.buckets { fmt.Printf("Bucket %d: ", i) current := node for current != nil { fmt.Printf("%d -> ", current.key) current = current.next } fmt.Println("null") } } func main() { ht := NewHashTable(10) ht.Insert(2) ht.Insert(12) ht.Insert(22) ht.Delete(12) ht.Print() }
Attempts:
2 left
💡 Hint
Deleting key 12 removes it from the linked list in bucket 2.
✗ Incorrect
Keys 2, 12, and 22 all hash to bucket 2. After deleting 12, the linked list in bucket 2 contains 2 -> 22 -> null.
🚀 Application
expertAnalyzing time complexity of chaining under heavy collisions
If a hash table uses chaining and all keys hash to the same bucket, what is the worst-case time complexity for searching a key in the table with n keys?
Attempts:
2 left
💡 Hint
Consider what happens if all keys collide into one bucket's linked list.
✗ Incorrect
When all keys collide into one bucket, the linked list contains all n keys, so searching requires checking each node, resulting in O(n) time.
