Collision handling is the method used 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 handles collisions by storing all elements that hash to the same index in a linked list at that index.
Click to reveal answer
beginner
What data structure is commonly used in chaining to store collided elements?
A linked list is commonly used to store collided elements at the same hash index in chaining.
Click to reveal answer
intermediate
How does chaining affect the average search time in a hash table?
Chaining keeps average search time close to O(1) if the load factor is low, but it can degrade to O(n) if many collisions occur.
Click to reveal answer
intermediate
Why is chaining preferred over open addressing in some cases?
Chaining is preferred because it handles collisions gracefully without clustering and allows the hash table to store more elements than its size.
Click to reveal answer
What happens in chaining when two keys hash to the same index?
AOne key replaces the other
BThe keys are stored in separate arrays
CThe table size is doubled
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 used in chaining for collision handling?
ALinked list
BQueue
CStack
DArray
✗ Incorrect
Chaining uses linked lists to store multiple elements at the same hash index.
What is the worst-case time complexity for search in chaining?
AO(1)
BO(log n)
CO(n)
DO(n log n)
✗ Incorrect
In the worst case, all elements hash to the same index, making search O(n).
How does chaining handle a hash table that is full?
AIt cannot store more elements
BIt stores collided elements in linked lists, allowing more elements than table size
CIt resizes the table automatically
DIt deletes old elements
✗ Incorrect
Chaining allows storing more elements than the table size by using linked lists at each index.
Which of these is a disadvantage of chaining?
ARequires extra memory for pointers
BCannot handle collisions
CAlways slower than open addressing
DDoes not allow duplicate keys
✗ Incorrect
Chaining requires extra memory to store pointers in linked lists.
Describe how collision handling using chaining works in a hash table.
Think about what happens when two keys get the same index.
You got /4 concepts.
Explain the advantages and disadvantages of using chaining for collision handling.
Consider memory use and search speed.
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 on collision
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 occurs.
Step 2: How chaining handles collision
Chaining stores all collided elements in a linked list at that index, allowing multiple elements.
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 for collisions [OK]
Common Mistakes:
Thinking collision replaces old element
Assuming resizing happens immediately on collision
Ignoring collided elements
2. Which of the following is the correct way to declare a hash table with chaining in Java using LinkedList?
easy
A. LinkedList<Integer> table = new LinkedList<>();
B. LinkedList<Integer>[] table = new LinkedList[10];
C. LinkedList<Integer>[] table = new LinkedList<>[10];
D. LinkedList<Integer> table = new LinkedList[10];
Solution
Step 1: Understand array of linked lists declaration
We need an array of LinkedList objects, so use LinkedList<Integer>[].
Step 2: Correct Java syntax for array of generic linked lists
Java allows new LinkedList[10] but not new LinkedList<>[10], so LinkedList<Integer>[] table = new LinkedList[10]; is correct.
Final Answer:
LinkedList<Integer>[] table = new LinkedList[10]; -> Option B
Quick Check:
Array of LinkedList = LinkedList[] table [OK]
Hint: Array of LinkedList uses LinkedList<Type>[] [OK]
Common Mistakes:
Trying to use new LinkedList<>[size]
Declaring LinkedList without array
Wrong generic syntax in array creation
3. Consider the following Java code snippet for a hash table with chaining:
import java.util.LinkedList;
public class HashTable {
LinkedList<Integer>[] table;
int size = 5;
public HashTable() {
table = new LinkedList[size];
for (int i = 0; i < size; i++) {
table[i] = new LinkedList<>();
}
}
public void insert(int key) {
int index = key % size;
table[index].add(key);
}
public void printTable() {
for (int i = 0; i < size; i++) {
System.out.print(i + ": ");
for (int val : table[i]) {
System.out.print(val + " -> ");
}
System.out.println("null");
}
}
public static void main(String[] args) {
HashTable ht = new HashTable();
ht.insert(7);
ht.insert(12);
ht.insert(17);
ht.printTable();
}
}
4. The following code snippet tries to insert keys into a hash table with chaining but throws a NullPointerException. What is the error?
import java.util.LinkedList;
public class HashTable {
LinkedList<Integer>[] table = new LinkedList[5];
public void insert(int key) {
int index = key % 5;
table[index].add(key);
}
public static void main(String[] args) {
HashTable ht = new HashTable();
ht.insert(10);
}
}
medium
A. The array of LinkedLists is not initialized with LinkedList objects
B. The modulo operation is incorrect
C. The insert method should return a value
D. The LinkedList should be replaced with ArrayList
Solution
Step 1: Identify cause of NullPointerException
The array 'table' is created but each element is null because LinkedList objects are not initialized.
Step 2: Fix by initializing each LinkedList in the array
Before inserting, each table[i] must be assigned new LinkedList<>() to avoid null pointer.
Final Answer:
The array of LinkedLists is not initialized with LinkedList objects -> Option A
5. You want to implement a hash table with chaining that supports insert and search operations. Given the following partial code, fill in the missing search method to return true if the key exists, otherwise false.
import java.util.LinkedList;
public class HashTable {
LinkedList<Integer>[] table;
int size = 7;
public HashTable() {
table = new LinkedList[size];
for (int i = 0; i < size; i++) {
table[i] = new LinkedList<>();
}
}
public void insert(int key) {
int index = key % size;
table[index].add(key);
}
// Fill this method
public boolean search(int key) {
// ???
}
}
Which of the following is the correct implementation of the search method?
hard
A. int index = key % size;
return table[index].contains(key);
B. int index = key % size;
return table[index].get(0) == key;
C. for (LinkedList<Integer> list : table) {
if (list.contains(key)) return true;
}
return false;
D. int index = key % size;
return table[index].indexOf(key) == -1;
Solution
Step 1: Calculate index using modulo
Use key % size to find the correct linked list index.
Step 2: Check if linked list at index contains the key
Use contains() method on linked list to check presence of key.
Final Answer:
int index = key % size;
return table[index].contains(key); -> Option A
Quick Check:
Search = contains at index [OK]
Hint: Search key in linked list at key % size [OK]
Common Mistakes:
Checking only first element in list
Searching all lists instead of one
Using indexOf == -1 incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.