📖 Scenario: Imagine you are organizing a small library. You want to quickly find books by their titles. To do this, you decide to use a simple hash table where each book title is linked to its shelf number.
🎯 Goal: You will create a hash table using a TypeScript object, write a simple hash function to convert book titles into shelf numbers, and then store and retrieve books using this hash function.
📋 What You'll Learn
Create a hash table as a TypeScript object with no initial entries.
Create a hash function called simpleHash that takes a string and returns a number.
Use the simpleHash function to assign shelf numbers to book titles.
Store book titles and their shelf numbers in the hash table.
Print the hash table to show the stored books and their shelf numbers.
💡 Why This Matters
🌍 Real World
Hash tables are used in libraries, databases, and many apps to quickly find information by keys like book titles or user names.
💼 Career
Understanding hash tables and hash functions is important for software developers to build fast and efficient data storage and retrieval systems.
Progress0 / 4 steps
1
Create an empty hash table
Create an empty object called hashTable to store book titles and their shelf numbers.
DSA Typescript
Hint
Use const hashTable: { [key: string]: number } = {}; to create an empty object.
2
Write a simple hash function
Create a function called simpleHash that takes a string key and returns a number. The function should add the Unicode values of all characters in key and return the sum modulo 10.
DSA Typescript
Hint
Use a for loop to add character codes and return the sum modulo 10.
3
Store books using the hash function
Use the simpleHash function to assign shelf numbers to these book titles: 'Math', 'Science', and 'History'. Store them in hashTable with the shelf number as the value and the book title as the key.
DSA Typescript
Hint
Call simpleHash with each book title and assign the result to hashTable[title].
4
Print the hash table
Print the hashTable object to show the stored book titles and their shelf numbers.
DSA Typescript
Hint
Use console.log(hashTable); to print the object.
Practice
(1/5)
1. What is the main purpose of a hash function in a hash table?
easy
A. To sort the keys in ascending order
B. To count the number of elements in the table
C. To encrypt the stored data for security
D. To convert a key into an index for storing values
Solution
Step 1: Understand the role of a hash function
A hash function takes a key and converts it into a number called an index.
Step 2: Connect index to storage in hash table
This index tells where to store or find the value in the hash table.
Final Answer:
To convert a key into an index for storing values -> Option D
Quick Check:
Hash function = key to index [OK]
Hint: Hash function maps keys to storage spots [OK]
Common Mistakes:
Thinking hash function sorts keys
Confusing hash function with encryption
Believing hash function counts elements
2. Which of the following is the correct way to declare a hash table (Map) in TypeScript?
easy
A. const map = new map();
B. const map = Map;
C. const map = new Map();
D. const map = new Map(); string, number;
Solution
Step 1: Recall TypeScript Map syntax
In TypeScript, a Map is created with new Map().
Step 2: Check each option's syntax
const map = new Map(); uses correct syntax: new Map(). Others have syntax errors or wrong usage.
Final Answer:
const map = new Map(); -> Option C
Quick Check:
Map declaration = new Map<Key, Value>() [OK]
Hint: Use new Map() to declare [OK]
Common Mistakes:
Omitting 'new' keyword
Using lowercase 'map' instead of 'Map'
Incorrect generic syntax
3. What will be the output of the following TypeScript code?
Setting a key that already exists updates its value.
Step 2: Trace the code
First 'apple' is set to 5, then updated to 10. So map.get('apple') returns 10.
Final Answer:
10 -> Option A
Quick Check:
Updated value = 10 [OK]
Hint: Last set value for key is stored [OK]
Common Mistakes:
Assuming first value stays
Thinking get returns undefined if key repeated
Confusing Map with object property behavior
4. Identify the error in the following TypeScript code using a hash table:
const map = new Map();
map.set(1, 'one');
map.set(2, 'two');
console.log(map.get('1'));
medium
A. Missing parentheses in map.get call
B. Using string '1' instead of number 1 as key
C. Map keys must be strings only
D. Cannot mix number and string values in Map
Solution
Step 1: Check Map key type
Map is declared with number keys, so keys must be numbers.
Step 2: Analyze map.get argument
map.get('1') uses string '1', which does not match number 1 key, so returns undefined.
Final Answer:
Using string '1' instead of number 1 as key -> Option B
Quick Check:
Key type mismatch causes undefined [OK]
Hint: Match key types exactly when using get() [OK]
Common Mistakes:
Assuming '1' and 1 are same keys
Thinking Map keys must be strings
Ignoring type mismatch errors
5. You want to design a hash function for strings that minimizes collisions for English words. Which approach is best?
hard
A. Multiply each character code by a prime number and sum, then modulo table size
B. Use the length of the string as the hash value
C. Sum the ASCII codes of all characters and take modulo table size
D. Use the first character's ASCII code only
Solution
Step 1: Understand collision minimization
Good hash functions spread keys evenly to reduce collisions.
Step 2: Evaluate each approach
Sum the ASCII codes of all characters and take modulo table size sums codes but can cause many collisions (e.g., 'abc' and 'cab'). Use the length of the string as the hash value uses length only, very poor. Use the first character's ASCII code only uses first char only, also poor. Multiply each character code by a prime number and sum, then modulo table size uses weighted sum with primes, better distribution.
Final Answer:
Multiply each character code by a prime number and sum, then modulo table size -> Option A
Quick Check:
Weighted sum with primes = better hash [OK]
Hint: Use prime multipliers for better hash spread [OK]
Common Mistakes:
Using simple sums causing collisions
Ignoring character positions
Using string length as hash
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.