📖 Scenario: Imagine you are building a simple phone book app. You want to store names and phone numbers. Sometimes, two names might have the same starting letter, causing a collision in your storage. You will use chaining to handle these collisions by storing multiple entries in a list at the same place.
🎯 Goal: Build a hash table using chaining to store names and phone numbers. You will create the initial hash table, add a helper variable for the hash size, insert entries handling collisions by chaining, and finally print the hash table.
📋 What You'll Learn
Create a hash table as a list of empty lists with a fixed size
Create a variable for the hash table size
Write a function to insert name and phone number into the hash table using chaining
Print the final hash table showing all chains
💡 Why This Matters
🌍 Real World
Hash tables with chaining are used in many apps to store data efficiently even when collisions happen, like phone books, caches, and databases.
💼 Career
Understanding collision handling is important for software engineers working on data storage, search engines, and performance optimization.
Progress0 / 4 steps
1
Create the hash table
Create a list called hash_table with 5 empty lists inside it to represent the buckets for chaining.
DSA Python
Hint
Use a list comprehension to create 5 empty lists inside hash_table.
2
Add the hash table size variable
Create a variable called hash_size and set it to 5 to represent the number of buckets in the hash table.
DSA Python
Hint
This variable will help us find the correct bucket for each entry.
3
Write the insert function using chaining
Write a function called insert that takes name and phone as parameters. Inside the function, calculate the index by taking the length of name modulo hash_size. Then append a tuple (name, phone) to the list at hash_table[index].
DSA Python
Hint
Use the length of the name to find the bucket index and add the entry as a tuple.
4
Insert entries and print the hash table
Use the insert function to add these entries: ("Amy", "123"), ("Bob", "456"), ("Cathy", "789"), ("Dan", "012"), and ("Eve", "345"). Then print the hash_table.
DSA Python
Hint
Check how names with the same length modulo 5 are grouped together in the same bucket.
Practice
(1/5)
1. What is the main idea behind collision handling using chaining in hash tables?
easy
A. Store collided elements in a linked list at the same index
B. Replace the existing element with the new one
C. Resize the hash table immediately
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 collisions
Chaining stores all collided elements in a linked list at that index, allowing multiple elements to share the same slot.
Final Answer:
Store collided elements in a linked list at the same index -> Option A
Quick Check:
Chaining = linked list at index [OK]
Hint: Remember: chaining uses linked lists to store collisions [OK]
Common Mistakes:
Thinking collisions replace existing elements
Assuming collisions are ignored
Believing resizing happens immediately on collision
2. Which of the following is the correct way to initialize a hash table with chaining in Python?
easy
A. hash_table = dict()
B. hash_table = [None] * size
C. hash_table = [0] * size
D. hash_table = [[] for _ in range(size)]
Solution
Step 1: Understand chaining initialization
Chaining requires each slot to hold a list to store multiple elements.
Step 2: Choose correct initialization
Using list comprehension to create a list of empty lists ensures each slot can hold multiple elements.
Final Answer:
hash_table = [[] for _ in range(size)] -> Option D
Quick Check:
Empty lists per slot = hash_table = [[] for _ in range(size)] [OK]
Hint: Use list of empty lists for chaining slots [OK]
Common Mistakes:
Using [None] * size creates references to same list
Using zeros instead of lists for chaining
Using dict() which is not a list-based hash table
3. Consider the following code using chaining for collision handling:
hash_table = [[] for _ in range(3)]
keys = [5, 8, 11]
for key in keys:
index = key % 3
hash_table[index].append(key)
print(hash_table)
What is the output?
medium
A. [[5, 8, 11], [], []]
B. [[], [], [5, 8, 11]]
C. [[], [5, 8, 11], []]
D. [[], [5], [8, 11]]
Solution
Step 1: Calculate indices for each key
5 % 3 = 2, 8 % 3 = 2, 11 % 3 = 2. All keys map to index 2.
Step 2: Append keys to corresponding lists
All keys 5, 8, 11 are appended to hash_table[2], so hash_table = [[], [], [5, 8, 11]].
Final Answer:
[[], [], [5, 8, 11]] -> Option B
Quick Check:
Keys mod 3 = 2, all in last list [OK]
Hint: Calculate key % size to find index, then append [OK]
Common Mistakes:
Miscomputing modulo operation
Appending keys to wrong index
Confusing list positions in output
4. Find the error in this chaining insertion code: