📖 Scenario: Imagine you are building a simple phone book app that stores names and phone numbers. To keep things fast, you use a hash table. But sometimes, two names might get the same spot in the table. This is called a collision. To fix this, you will use chaining, which means storing multiple entries in a list at the same spot.
🎯 Goal: You will create a hash table using chaining to handle collisions. You will add entries and then print the table to see how collisions are handled.
📋 What You'll Learn
Create a hash table with a fixed size
Implement a simple hash function
Use chaining (arrays) to handle collisions
Add multiple entries to the hash table
Print the hash table showing chains
💡 Why This Matters
🌍 Real World
Hash tables with chaining are used in many apps to store and quickly find data like usernames, phone numbers, or product info even when collisions happen.
💼 Career
Understanding collision handling is important for software developers working with databases, caching systems, or any data storage that needs fast access.
Progress0 / 4 steps
1
Create the hash table array
Create a variable called hashTable that is an array with 5 empty arrays inside it.
DSA Javascript
Hint
Think of the hash table as 5 buckets, each bucket can hold many items.
2
Create a simple hash function
Create a function called hash that takes a string key and returns the sum of the character codes modulo 5.
DSA Javascript
Hint
Loop over each character in the key, add its code to sum, then use modulo 5 to get the index.
3
Add entries using chaining
Write a function called addEntry that takes key and value. Use the hash function to find the index. Then push an object with key and value into the array at that index in hashTable.
DSA Javascript
Hint
Use the hash function to find the bucket, then add the key and value as an object to that bucket's array.
4
Add entries and print the hash table
Use addEntry to add these entries: 'Alice' with '123', 'Bob' with '456', 'Charlie' with '789', 'David' with '101', and 'Eve' with '112'. Then print hashTable.
DSA Javascript
Hint
Call addEntry for each name and number, then print the whole hashTable to see the chains.
Practice
(1/5)
1. What is the main idea behind collision handling using chaining in hash tables?
easy
A. Resize the hash table immediately after every insertion
B. Replace the existing element with the new one when a collision happens
C. Store multiple elements in a linked list at the same index when collisions occur
D. Use a secondary hash function to find a new index
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.
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 list at one index for collisions [OK]
Common Mistakes:
Thinking chaining replaces old values
Confusing chaining with open addressing
Assuming resizing happens on every collision
2. Which of the following is the correct way to initialize a hash table with chaining in JavaScript?
Hint: Avoid fill([]); use Array.from with function [OK]
Common Mistakes:
Changing hash function unnecessarily
Using wrong array methods
Ignoring shared reference problem
5. You have a hash table of size 4 using chaining. You insert keys [3, 7, 11, 15, 19]. The hash function is key % 4. After all insertions, which index has the longest chain and what is its content?