Challenge - 5 Problems
Hash Table Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this hash function simulation?
Consider a simple hash function that sums the character codes of a string and then takes modulo 10. What is the output of the hash function for the key 'abc'?
DSA Javascript
function simpleHash(key) {
let sum = 0;
for (let i = 0; i < key.length; i++) {
sum += key.charCodeAt(i);
}
return sum % 10;
}
console.log(simpleHash('abc'));Attempts:
2 left
💡 Hint
Add the ASCII codes of 'a', 'b', and 'c' and then take modulo 10.
✗ Incorrect
The ASCII codes are: 'a' = 97, 'b' = 98, 'c' = 99. Sum is 97 + 98 + 99 = 294. 294 % 10 = 4, which is option B.
❓ Predict Output
intermediateWhat is the output of this hash table insertion simulation?
Given a hash table of size 5 and a hash function that returns key % 5, what is the final state of the table after inserting keys 1, 6, and 11 using linear probing?
DSA Javascript
const size = 5; const table = new Array(size).fill(null); function hash(key) { return key % size; } function insert(key) { let idx = hash(key); while (table[idx] !== null) { idx = (idx + 1) % size; } table[idx] = key; } insert(1); insert(6); insert(11); console.log(table);
Attempts:
2 left
💡 Hint
Check the hash index for each key and apply linear probing if collision occurs.
✗ Incorrect
Key 1 hashes to 1 % 5 = 1, placed at index 1. Key 6 hashes to 6 % 5 = 1, collision at index 1, linear probe to index 2. Key 11 hashes to 11 % 5 = 1, collision at 1 and 2, linear probe to index 3.
🧠 Conceptual
advancedWhich property is NOT desirable in a good hash function?
Select the property that is NOT desirable when designing a hash function for a hash table.
Attempts:
2 left
💡 Hint
Think about efficiency and speed in hash functions.
✗ Incorrect
A good hash function should be fast and efficient, so high computational complexity is undesirable.
🔧 Debug
advancedWhat error does this hash table insertion code produce?
Identify the error when inserting keys into a fixed-size hash table using the given code.
DSA Javascript
const size = 3; const table = new Array(size).fill(null); function hash(key) { return key % size; } function insert(key) { let idx = hash(key); let startIdx = idx; while (table[idx] !== null) { idx = (idx + 1) % size; if (idx === startIdx) { throw new Error('Hash table is full'); } } table[idx] = key; } insert(1); insert(4); insert(7); try { insert(10); } catch (e) { console.error(e.message); } console.log(table);
Attempts:
2 left
💡 Hint
Consider what happens when the table is full and you try to insert another key.
✗ Incorrect
The table size is 3, but 4 keys are inserted. The insert function loops infinitely trying to find an empty slot.
🚀 Application
expertHow many unique keys can be stored without collision in a hash table of size 7 using this hash function?
Given a hash function h(key) = key % 7 and a hash table of size 7, what is the maximum number of unique integer keys that can be inserted without any collision?
Attempts:
2 left
💡 Hint
Think about the pigeonhole principle and the size of the table.
✗ Incorrect
Since the table size is 7 and the hash function maps keys to 7 slots, at most 7 unique keys can be inserted without collision.
