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 in collision handling.
Chaining stores all elements that hash to the same index in a linked list at that index, allowing multiple items to be stored in one bucket.
Click to reveal answer
beginner
What data structure is commonly used in chaining?
A linked list is commonly used to store multiple elements at the same hash index in chaining.
Click to reveal answer
intermediate
How does chaining affect search time in a hash table?
Search time may increase if many elements collide and form a long linked list, but on average it remains efficient if the load factor is low.
Click to reveal answer
intermediate
What is the main advantage of chaining over open addressing?
Chaining can handle an unlimited number of collisions by growing linked lists, while open addressing has limited slots and may require rehashing.
Click to reveal answer
In chaining, what happens when two keys hash to the same index?
AThe second key is discarded
BOne key replaces the other
CThe hash table resizes immediately
DBoth keys are stored in a linked list at that index
✗ Incorrect
Chaining stores all colliding keys in a linked list at the same index.
Which data structure is used in chaining to handle collisions?
AArray
BQueue
CLinked list
DStack
✗ Incorrect
Linked lists are used to store multiple elements at the same hash index in chaining.
What is a potential downside of chaining?
AIt can cause long linked lists slowing down search
BIt cannot handle collisions
CIt wastes memory by storing empty slots
DIt requires rehashing on every insert
✗ Incorrect
Long linked lists from many collisions can slow down search operations.
How does chaining handle unlimited collisions?
ABy storing colliding elements in linked lists
BBy resizing the hash table
CBy discarding extra elements
DBy using open addressing
✗ Incorrect
Chaining uses linked lists to store all colliding elements at the same index.
What is the load factor in a hash table?
ANumber of collisions
BNumber of keys divided by number of buckets
CSize of linked list
DHash function complexity
✗ Incorrect
Load factor is the ratio of keys stored to the number of buckets in the hash table.
Describe how chaining resolves collisions in a hash table.
Think about what happens when two friends want to sit on the same chair.
You got /4 concepts.
Explain the advantages and disadvantages of using chaining for collision handling.
Consider both how chaining helps and what problems it might cause.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind collision handling using chaining in hash tables?
easy
A. Replace the existing element with the new one
B. Resize the hash table immediately when collision occurs
C. Store collided elements in a linked list at the same index
D. Ignore the new element causing collision
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 collided elements in a linked list at the same index -> Option C
Quick Check:
Chaining = linked list at index [OK]
Hint: Chaining means linked list at one index [OK]
Common Mistakes:
Thinking chaining replaces old elements
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 C# using List<T> for chaining?
easy
A. Dictionary hashTable = new Dictionary();
B. Dictionary hashTable = new Dictionary();
C. List hashTable = new List();
D. Dictionary hashTable = new Dictionary();
Solution
Step 1: Understand chaining data structure
Chaining uses a list to store multiple values per key index.
Step 2: Correct C# syntax for dictionary with list values
Dictionary means keys are int, values are lists of strings.
Final Answer:
Dictionary hashTable = new Dictionary(); -> Option D
Quick Check:
Dictionary with List values = Dictionary hashTable = new Dictionary(); [OK]
Hint: Use Dictionary with List as value for chaining [OK]
Common Mistakes:
Using List with two generic types (invalid)
Using Dictionary with simple string values only
Confusing key and value types
3. What will be the output of the following C# code snippet using chaining?
var hashTable = new Dictionary();
int key = 2;
if (!hashTable.ContainsKey(key))
hashTable[key] = new List<string>();
hashTable[key].Add("apple");
hashTable[key].Add("banana");
foreach(var item in hashTable[key])
Console.Write(item + " ");
medium
A. No output
B. apple banana
C. apple
D. banana apple
Solution
Step 1: Initialize dictionary and add list for key 2
Since key 2 is not present, a new list is created and assigned.
Step 2: Add "apple" and "banana" to list at key 2
Both strings are added in order: "apple", then "banana".
Step 3: Print all items in list at key 2
Loop prints "apple " then "banana " in order.
Final Answer:
apple banana -> Option B
Quick Check:
List order preserved = apple banana [OK]
Hint: Items print in insertion order in chaining list [OK]
Common Mistakes:
Assuming reversed order output
Forgetting to initialize list before adding
Expecting no output if key missing
4. Identify the error in this C# code snippet implementing chaining:
var hashTable = new Dictionary();
int key = 5;
hashTable[key].Add("orange");
medium
A. Key does not exist; list not initialized before Add
B. Dictionary cannot have List as value type
C. Add method does not exist for List
D. Key must be string, not int
Solution
Step 1: Check if key 5 exists in dictionary
Key 5 is not added yet, so hashTable[key] is null.
Step 2: Adding to null list causes runtime error
Cannot call Add on null; list must be initialized first.
Final Answer:
Key does not exist; list not initialized before Add -> Option A
Quick Check:
Initialize list before Add [OK]
Hint: Always initialize list before adding elements [OK]
Common Mistakes:
Assuming dictionary auto-creates lists
Confusing key types
Thinking Add method is invalid
5. You want to implement a hash table with chaining in C# that stores multiple values per key and supports removing a specific value from the chain. Which approach correctly supports this feature?
hard
A. Use Dictionary and call Remove(value) on the list at the key
B. Use Dictionary and overwrite value to remove
C. Use Dictionary but clear entire list to remove one value
D. Use Dictionary but do not implement removal
Solution
Step 1: Understand chaining with multiple values per key
Chaining uses a list to hold multiple values at one key.
Step 2: Removing a specific value requires list's Remove method
List<string> supports Remove(value) to delete one item without affecting others.
Step 3: Other options either overwrite or clear all values, not specific removal
Overwriting loses all values; clearing removes all; ignoring removal is incomplete.
Final Answer:
Use Dictionary and call Remove(value) on the list at the key -> Option A
Quick Check:
List Remove supports specific value deletion [OK]
Hint: Use List and Remove(value) to delete specific chained item [OK]
Common Mistakes:
Overwriting value instead of removing one
Clearing entire list to remove one item
Ignoring removal feature
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.