Collision Handling Using Chaining in DSA Java - Time & Space Complexity
When we use chaining to handle collisions in a hash table, we want to know how fast operations like search, insert, and delete run.
We ask: How does the time to do these operations grow as the number of items increases?
Analyze the time complexity of the following code snippet.
import java.util.LinkedList;
class HashTable {
LinkedList[] table;
int hash(int key) {
return key % table.length;
}
void insert(int key) {
int index = hash(key);
table[index].add(key); // add to linked list at index
}
boolean search(int key) {
int index = hash(key);
return table[index].contains(key); // search linked list
}
}
This code inserts and searches keys in a hash table using chaining, where each bucket holds a linked list of keys.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the linked list at a hash bucket during search or insert.
- How many times: Depends on the number of keys in that bucket, which varies with collisions.
As more keys are added, some buckets get longer linked lists, so searching or inserting takes longer.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1 to 2 steps per search (few collisions) |
| 100 | About 5 to 10 steps per search (more collisions) |
| 1000 | About 50 to 100 steps per search (many collisions) |
Pattern observation: If the number of buckets stays fixed, the linked lists grow longer roughly proportional to n, so operations slow down linearly.
Time Complexity: O(n/k) where k is number of buckets, often simplified to O(n) in worst case
This means if many keys collide into the same bucket, operations take longer, growing linearly with the number of keys in that bucket.
[X] Wrong: "Hash table operations always take constant time, no matter what."
[OK] Correct: If many keys collide into the same bucket, the linked list grows, making search or insert slower than constant time.
Understanding how chaining affects time complexity helps you explain hash table performance clearly and shows you know how collisions impact speed in real systems.
"What if we used balanced trees instead of linked lists for chaining? How would the time complexity change?"
Practice
collision handling using chaining in hash tables?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 CQuick Check:
Chaining = linked list at index [OK]
- Thinking collision replaces old element
- Assuming resizing happens immediately on collision
- Ignoring collided elements
LinkedList?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 BQuick Check:
Array of LinkedList = LinkedList[] table [OK]
- Trying to use new LinkedList<>[size]
- Declaring LinkedList without array
- Wrong generic syntax in array creation
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();
}
}
What is the output of this program?Solution
Step 1: Calculate index for each inserted key
Size is 5, so index = key % 5. 7 % 5 = 2, 12 % 5 = 2, 17 % 5 = 2.Step 2: Insert keys at index 2 and print table
All keys go to table[2]. So index 2 has 7 -> 12 -> 17 -> null, others are empty.Final Answer:
0: null 1: null 2: 7 -> 12 -> 17 -> null 3: null 4: null -> Option DQuick Check:
Keys mod 5 = 2, all at index 2 [OK]
- Miscomputing modulo operation
- Assuming keys go to different indices
- Confusing index with key value
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);
}
}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 AQuick Check:
NullPointerException = uninitialized LinkedList array [OK]
- Ignoring initialization of linked lists
- Blaming modulo operation
- Thinking insert needs return value
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?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 AQuick Check:
Search = contains at index [OK]
- Checking only first element in list
- Searching all lists instead of one
- Using indexOf == -1 incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
