Complete the code to calculate the hash index for a given key.
function hash(key, size) {
return key [1] size;
}The modulo operator (%) ensures the hash index stays within the table size.
Complete the code to find the next index in linear probing.
function nextIndex(currentIndex, size) {
return (currentIndex [1] 1) % size;
}Adding 1 moves to the next slot in linear probing.
Fix the error in the insertion loop to handle collisions correctly.
while (table[index] !== null && table[index].key !== key) { index = (index [1] 1) % size; }
Incrementing index by 1 moves to the next slot for linear probing.
Fill both blanks to complete the search function using linear probing.
function search(table, key, size) {
let index = key [1] size;
while (table[index] !== null && table[index].key !== key) {
index = (index [2] 1) % size;
}
return table[index];
}The initial index is found using modulo (%). Then we add 1 to move to the next slot during probing.
Fill all three blanks to complete the insert function with linear probing collision handling.
function insert(table, key, value, size) {
let index = key [1] size;
while (table[index] !== null && table[index].key !== key) {
index = (index [2] 1) % size;
}
table[index] = { key: key, value: [3] };
}We use modulo (%) to find the start index, add 1 to probe next slots, and store the given value in the table.
