Collision handling is a method to resolve situations when two keys hash to the same index in a hash table.
Click to reveal answer
beginner
Explain chaining as a collision handling technique.
Chaining stores multiple elements at the same index using a linked list or similar structure, allowing all collided keys to be stored together.
Click to reveal answer
beginner
What data structure is commonly used in chaining to handle collisions?
A linked list is commonly used to store all elements that hash to the same index in chaining.
Click to reveal answer
intermediate
How does chaining affect the average time complexity of search in a hash table?
With chaining, average search time is O(1 + α), where α is the load factor (number of elements divided by number of buckets).
Click to reveal answer
intermediate
Why is chaining preferred over open addressing in some cases?
Chaining handles collisions gracefully even when the table is full, and it is easier to implement deletion of elements.
Click to reveal answer
What happens when two keys hash to the same index in chaining?
AOne key replaces the other
BThe second key is discarded
CThe hash table resizes immediately
DBoth keys are stored in a linked list at that index
✗ Incorrect
In chaining, collided keys are stored in a linked list at the same index.
Which data structure is typically used in chaining to store collided elements?
ALinked list
BArray
CStack
DQueue
✗ Incorrect
Linked lists are used to store multiple elements at the same index in chaining.
What is the load factor in a hash table?
ANumber of elements divided by number of buckets
BNumber of collisions divided by number of elements
CNumber of buckets divided by number of elements
DNumber of keys hashed
✗ Incorrect
Load factor is the ratio of elements to buckets in a hash table.
How does chaining handle deletion of elements?
ADeletion is not possible in chaining
BBy removing the element from the linked list at the index
CBy rehashing the entire table
DBy marking the element as deleted but keeping it
✗ Incorrect
Elements can be removed directly from the linked list in chaining.
What is the average search time complexity in chaining when load factor is low?
AO(n)
BO(log n)
CO(1)
DO(n log n)
✗ Incorrect
When load factor is low, search time in chaining is close to O(1).
Describe how chaining resolves collisions in a hash table.
Think about how multiple items can share the same bucket.
You got /3 concepts.
Explain the impact of load factor on the performance of chaining.
Consider how crowded the buckets get.
You got /3 concepts.
Practice
(1/5)
1. What is the main idea behind collision handling using chaining in hash tables?
easy
A. Use open addressing to find another empty slot
B. Resize the hash table to avoid collisions
C. Store multiple elements in a linked list at the same index when collisions occur
D. Ignore collisions and overwrite existing data
Solution
Step 1: Understand collision in hash tables
When two keys hash to the same index, a collision happens.
Step 2: How chaining handles collisions
Chaining stores all collided elements in a linked list at that index.
Final Answer:
Store multiple elements in a linked list at the same index when collisions occur -> Option C
Quick Check:
Chaining = linked list at index [OK]
Hint: Chaining means linked lists at each index [OK]
Common Mistakes:
Thinking chaining resizes the table
Confusing chaining with open addressing
Assuming collisions are ignored
2. Which of the following is the correct way to declare a hash table with chaining in Go using slices of linked lists?
easy
A. var table [10]*Node
B. var table = make([]*Node, 10)
C. var table = make(map[int]*Node)
D. var table = [10]Node{}
Solution
Step 1: Understand Go slice declaration for chaining
Chaining uses a slice of pointers to linked list nodes for dynamic size.
Step 2: Identify correct syntax
Using make([]*Node, 10) creates a slice of 10 pointers to Node, suitable for chaining.
Final Answer:
var table = make([]*Node, 10) -> Option B
Quick Check:
Slice of pointers = make([]*Node, size) [OK]
Hint: Use make([]*Node, size) for chaining table [OK]
Common Mistakes:
Using arrays instead of slices limits flexibility
Using map instead of slice for chaining
Declaring slice without make causes nil slice
3. Consider the following Go code snippet for inserting keys into a hash table with chaining. What will be the output after inserting keys 5 and 15 if the hash function is key % 10?
type Node struct {
key int
next *Node
}
func insert(table []*Node, key int) {
index := key % 10
newNode := &Node{key: key}
newNode.next = table[index]
table[index] = newNode
}
func printTable(table []*Node) {
for i, node := range table {
print(i, ": ")
for n := node; n != nil; n = n.next {
print(n.key, " -> ")
}
println("nil")
}
}
func main() {
table := make([]*Node, 10)
insert(table, 5)
insert(table, 15)
printTable(table)
}
medium
A. 15 -> 5 -> nil at index 5
B. 15 -> nil at index 5 and 5 -> nil at index 5
C. 5 -> nil at index 5 and 15 -> nil at index 5
D. 5 -> 15 -> nil at index 5
Solution
Step 1: Calculate hash index for keys 5 and 15
Both 5 % 10 and 15 % 10 equal 5, so both keys go to index 5.
Step 2: Trace insertion order in chaining
First insert 5 at index 5, then insert 15 which becomes new head pointing to 5.
Final Answer:
15 -> 5 -> nil at index 5 -> Option A
Quick Check:
New nodes added at head = 15 first [OK]
Hint: New nodes go to head of list in chaining [OK]
Common Mistakes:
Assuming insertion appends at tail
Mixing up hash index calculation
Printing nodes in wrong order
4. The following Go code tries to insert keys into a hash table with chaining but causes a runtime panic. What is the error?
Must create a new Node and assign pointer to table[index] before setting key.
Final Answer:
Nil pointer dereference when assigning to table[index].key -> Option A
Quick Check:
Assigning field on nil pointer causes panic [OK]
Hint: Initialize pointer before assigning fields [OK]
Common Mistakes:
Ignoring nil pointer before field assignment
Assuming slice elements auto-initialize
Confusing compile error with runtime panic
5. You want to implement a hash table with chaining in Go that supports deletion of keys. Which approach correctly removes a key from the chain at the hashed index?
hard
A. Replace the key with zero but keep the node in the list
B. Set the entire linked list at the index to nil to remove the key
C. Resize the hash table and rehash all keys except the one to delete
D. Traverse the linked list at the index, keep track of previous node, and adjust pointers to skip the node with the key
Solution
Step 1: Understand deletion in chaining
To delete a key, find it in the linked list and remove its node by adjusting pointers.
Step 2: Why other options fail
Setting entire list to nil removes all keys, replacing key with zero keeps node, resizing is inefficient.
Final Answer:
Traverse the linked list at the index, keep track of previous node, and adjust pointers to skip the node with the key -> Option D
Quick Check:
Proper deletion adjusts pointers in linked list [OK]
Hint: Delete by pointer adjustment in linked list [OK]
Common Mistakes:
Removing entire chain instead of one key
Leaving empty nodes with zero keys
Rehashing unnecessarily on deletion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.