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 a chained hash table
Consider a hash table of size 3 using chaining for collision handling. The hash function is
key % 3. Insert keys 2, 5, 8 in this order. What is the state of the hash table after these insertions?DSA Typescript
class Node { constructor(public key: number, public next: Node | null = null) {} } class HashTable { table: Array<Node | null> = [null, null, null]; hash(key: number): number { return key % 3; } insert(key: number): void { const index = this.hash(key); const newNode = new Node(key); if (!this.table[index]) { this.table[index] = newNode; } else { newNode.next = this.table[index]; this.table[index] = newNode; } } print(): string { return this.table.map((head, i) => { let result = `${i}: `; let current = head; while (current) { result += `${current.key} -> `; current = current.next; } return result + 'null'; }).join(' | '); } } const ht = new HashTable(); ht.insert(2); ht.insert(5); ht.insert(8); console.log(ht.print());
Attempts:
2 left
💡 Hint
Remember the hash function key % 3 and that new nodes are inserted at the head of the chain.
✗ Incorrect
The hash function key % 3 maps keys 2, 5, and 8 to index 2. Each new key is inserted at the head of the linked list at index 2, so the order is 8 -> 5 -> 2 -> null.
🧠 Conceptual
intermediateUnderstanding the effect of chaining on hash table search time
In a hash table using chaining, if the load factor (number of keys / table size) increases significantly, what happens to the average search time for a key?
Attempts:
2 left
💡 Hint
Think about how many elements are in each chain as more keys are added.
✗ Incorrect
As the load factor increases, more keys hash to the same index, making chains longer. Searching requires traversing these chains, increasing average search time.
🔧 Debug
advancedIdentify the bug in chaining insertion code
The following TypeScript code attempts to insert keys into a hash table using chaining. It compiles but does not insert keys correctly. What is the bug?
DSA Typescript
class Node { constructor(public key: number, public next: Node | null = null) {} } class HashTable { table: Array<Node | null> = [null, null, null]; hash(key: number): number { return key % 3; } insert(key: number): void { const index = this.hash(key); const newNode = new Node(key); if (this.table[index]) { this.table[index] = newNode; } else { newNode.next = this.table[index]; this.table[index] = newNode; } } }
Attempts:
2 left
💡 Hint
Check what happens when the bucket already has nodes.
✗ Incorrect
The insert method overwrites the existing chain when the bucket is not empty, losing all previous nodes. The condition should insert new nodes at the head when the bucket is not empty.
📝 Syntax
advancedIdentify the syntax error in chaining insertion code
Which option contains a syntax error in the chaining insertion method?
DSA Typescript
insert(key: number): void {
const index = this.hash(key);
const newNode = new Node(key);
if (!this.table[index]) {
this.table[index] = newNode;
} else {
newNode.next = this.table[index];
this.table[index] = newNode;
}
}Attempts:
2 left
💡 Hint
Check the syntax of the if statement.
✗ Incorrect
Option A is missing parentheses around the if condition, causing a syntax error.
🚀 Application
expertResulting chain after multiple insertions and deletions
A hash table of size 4 uses chaining with the hash function
key % 4. Insert keys 3, 7, 11, 15 in this order. Then delete key 7. What is the chain at index 3 after these operations?DSA Typescript
class Node { constructor(public key: number, public next: Node | null = null) {} } class HashTable { table: Array<Node | null> = [null, null, null, null]; hash(key: number): number { return key % 4; } insert(key: number): void { const index = this.hash(key); const newNode = new Node(key); newNode.next = this.table[index]; this.table[index] = newNode; } delete(key: number): void { const index = this.hash(key); let current = this.table[index]; let prev: Node | null = null; while (current) { if (current.key === key) { if (prev) { prev.next = current.next; } else { this.table[index] = current.next; } return; } prev = current; current = current.next; } } printChain(index: number): string { let current = this.table[index]; let result = ''; while (current) { result += `${current.key} -> `; current = current.next; } return result + 'null'; } } const ht = new HashTable(); ht.insert(3); ht.insert(7); ht.insert(11); ht.insert(15); ht.delete(7); console.log(ht.printChain(3));
Attempts:
2 left
💡 Hint
Remember insertions add nodes at the head and deletion removes the matching key.
✗ Incorrect
All keys hash to index 3 (3 % 4 = 3, 7 % 4 = 3, 11 % 4 = 3, 15 % 4 = 3). Insertions add at head, so chain before deletion is 15 -> 11 -> 7 -> 3 -> null. Deleting 7 removes it from the chain, resulting in 15 -> 11 -> 3 -> null.
