Challenge - 5 Problems
Chaining Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting keys with collisions?
Consider a hash table using chaining with a simple hash function:
hash(key) = key % 3.
We insert keys 3, 6, 9, and 4 in that order.
What is the state of the hash table's buckets after these insertions?
hash(key) = key % 3.
We insert keys 3, 6, 9, and 4 in that order.
What is the state of the hash table's buckets after these insertions?
DSA Javascript
class HashTable { constructor() { this.buckets = [[], [], []]; } hash(key) { return key % 3; } insert(key) { const index = this.hash(key); this.buckets[index].push(key); } } const ht = new HashTable(); ht.insert(3); ht.insert(6); ht.insert(9); ht.insert(4); console.log(ht.buckets);
Attempts:
2 left
💡 Hint
Remember the hash function divides the key by 3 and uses the remainder as the bucket index.
✗ Incorrect
Keys 3, 6, and 9 all have remainder 0 when divided by 3, so they go into bucket 0. Key 4 has remainder 1, so it goes into bucket 1. Bucket 2 remains empty.
🧠 Conceptual
intermediateWhy is chaining used in collision handling?
In hash tables, collisions happen when multiple keys map to the same bucket. Why do we use chaining to handle collisions?
Attempts:
2 left
💡 Hint
Think about how multiple items can be stored in the same bucket without losing data.
✗ Incorrect
Chaining keeps all keys that hash to the same bucket in a list (like a linked list), so collisions don't overwrite data.
🔧 Debug
advancedFind the bug in this chaining insertion code
This code tries to insert keys into a hash table using chaining. It should add keys to the correct bucket. What is the bug?
DSA Javascript
class HashTable { constructor(size) { this.buckets = new Array(size).fill([]); } hash(key) { return key % this.buckets.length; } insert(key) { const index = this.hash(key); this.buckets[index].push(key); } } const ht = new HashTable(3); ht.insert(1); ht.insert(4); console.log(ht.buckets);
Attempts:
2 left
💡 Hint
Check how the buckets array is initialized and if each bucket is independent.
✗ Incorrect
Using fill([]) assigns the same array reference to all buckets, so pushing to one bucket affects all buckets.
❓ Predict Output
advancedWhat is the output after deleting a key in chaining?
Given a hash table with chaining, after inserting keys 2, 5, 8 and then deleting key 5, what is the state of the buckets?
DSA Javascript
class HashTable { constructor() { this.buckets = [[], [], []]; } hash(key) { return key % 3; } insert(key) { const index = this.hash(key); this.buckets[index].push(key); } delete(key) { const index = this.hash(key); this.buckets[index] = this.buckets[index].filter(k => k !== key); } } const ht = new HashTable(); ht.insert(2); ht.insert(5); ht.insert(8); ht.delete(5); console.log(ht.buckets);
Attempts:
2 left
💡 Hint
Check the hash values of keys 2, 5, and 8 and which buckets they belong to.
✗ Incorrect
2 % 3 = 2, 5 % 3 = 2, 8 % 3 = 2, so all keys go to bucket 2. After deleting 5, bucket 2 has [2,8]. Buckets 0 and 1 are empty.
🧠 Conceptual
expertWhat is the average time complexity of search in chaining with a good hash function?
Assuming a hash table uses chaining and a good hash function that distributes keys evenly, what is the average time complexity to search for a key?
Attempts:
2 left
💡 Hint
Think about how chaining works when keys are spread evenly across buckets.
✗ Incorrect
With a good hash function, each bucket has few keys, so searching a chain is fast and average time is constant.
