📖 Scenario: Imagine you are building a simple phone book app. You want to store names and phone numbers so you can find a number quickly by name. But sometimes, two names might hash to the same spot in your storage. To handle this, you will use chaining: each spot holds a list of entries that share the same hash.
🎯 Goal: You will create a hash table using chaining to store and retrieve phone numbers by name. You will add entries, handle collisions by chaining, and then print the stored phone book.
📋 What You'll Learn
Create a hash table with chaining using an array of lists
Add entries with names and phone numbers
Handle collisions by adding entries to the list at the hashed index
Print the full phone book showing all chains
💡 Why This Matters
🌍 Real World
Hash tables with chaining are used in many software systems to store and quickly find data even when keys collide.
💼 Career
Understanding collision handling is important for software engineers working on databases, caches, and any system requiring fast data lookup.
Progress0 / 4 steps
1
Create the hash table array
Create a variable called hashTable that is an array with 5 empty arrays inside it to hold chained entries.
DSA Typescript
Hint
Think of hashTable as a shelf with 5 boxes. Each box can hold many name-phone pairs.
2
Create a simple hash function
Create a function called hash that takes a key string and returns a number between 0 and 4 by summing the character codes of key and taking modulo 5.
DSA Typescript
Hint
This function turns a name into a number from 0 to 4 to find the right box on the shelf.
3
Add entries to the hash table with chaining
Create a function called addEntry that takes name and phone strings, computes the index using hash(name), and pushes the pair [name, phone] into the array at hashTable[index].
DSA Typescript
Hint
This function puts the name and phone number into the right box on the shelf, adding to the list if there are others.
4
Print the full phone book with chains
Use a for loop with variable i from 0 to 4 to print each index and its chained entries in hashTable. Format each line as Index i: name1 -> phone1, name2 -> phone2 or Index i: empty if no entries.
DSA Typescript
Hint
Print each box number and the list of names and phones chained there. Show 'empty' if no entries.
Practice
(1/5)
1. What is the main idea behind collision handling using chaining in a hash table?
easy
A. Store all elements that hash to the same index in a linked list at that index
B. Replace the existing element with the new one when a collision occurs
C. Use a secondary hash function to find a new index
D. Resize the hash table immediately when a collision happens
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 per slot.
Final Answer:
Store all elements that hash to the same index in a linked list at that index -> Option A
Quick Check:
Chaining = linked list at index [OK]
Hint: Chaining means linked list at each hash index [OK]
Common Mistakes:
Thinking chaining replaces old elements
Confusing chaining with open addressing
Assuming chaining resizes table on collision
2. Which of the following TypeScript code snippets correctly initializes a hash table with chaining?
easy
A. const table: Array = new Array(10).fill([]);
B. const table: Array<number> = Array(10).map(() => []);
C. const table: Array<number> = new Array(10).fill(0);
Step 1: Understand how to initialize array of arrays in TypeScript
Using Array.from with a function creates separate arrays for each slot.
Step 2: Analyze each option
const table: Array = new Array(10).fill([]); uses fill([]) which fills all slots with the same array reference (wrong). const table: Array = Array.from({ length: 10 }, () => []); correctly creates separate empty arrays. const table: Array<number> = new Array(10).fill(0); is a number array, not array of arrays. const table: Array<number> = Array(10).map(() => []); uses map on uninitialized array, which won't work.
Hint: Avoid fill([]); use Array.from with function for unique arrays [OK]
Common Mistakes:
Thinking hash function is wrong
Trying to fix by changing push to concat
Ignoring array reference sharing issue
5. You have a hash table of size 4 using chaining. You insert keys 2, 6, 10, 14, and 18. Then you want to remove key 10. Which of the following shows the correct state of the hash table chains after removal?
Hash function: key % 4
hard
A. [[2, 6, 14, 18], [ ], [ ], [ ]] with 10 removed
B. [[2, 6, 14, 18], [ ], [ ], [ ]]
C. [[ ], [ ], [2, 6, 14, 18], [ ]] with 10 removed