Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the hash index for a given key.
DSA Typescript
const hash = (key: number, size: number): number => {
return key [1] size;
};Attempts:
3 left
💡 Hint
Common Mistakes
Using + or * will cause index out of range errors.
✗ Incorrect
The modulo operator (%) is used to ensure the hash index fits within the table size.
2fill in blank
mediumComplete the code to find the next index using linear probing.
DSA Typescript
const linearProbe = (currentIndex: number, size: number): number => {
return (currentIndex [1] 1) % size;
};Attempts:
3 left
💡 Hint
Common Mistakes
Using - or * will not correctly move to the next slot.
✗ Incorrect
Adding 1 moves to the next slot in linear probing.
3fill in blank
hardFix the error in the insert function to handle collisions using linear probing.
DSA Typescript
function insert(hashTable: (number | null)[], key: number): void {
const size = hashTable.length;
let index = key [1] size;
while (hashTable[index] !== null) {
index = (index [2] 1) % size;
}
hashTable[index] = key;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using * or - causes wrong index calculation or infinite loops.
✗ Incorrect
Use modulo (%) to get initial index and +1 for linear probing next index.
4fill in blank
hardFill both blanks to complete the search function using linear probing.
DSA Typescript
function search(hashTable: (number | null)[], key: number): number {
const size = hashTable.length;
let index = key [1] size;
let startIndex = index;
while (hashTable[index] !== null) {
if (hashTable[index] === key) {
return index;
}
index = (index [2] 1) % size;
if (index === startIndex) break;
}
return -1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using - or * breaks the probing sequence or causes errors.
✗ Incorrect
Modulo (%) for initial index and +1 for next index in linear probing.
5fill in blank
hardFill all three blanks to complete the delete function using linear probing.
DSA Typescript
function deleteKey(hashTable: (number | null)[], key: number): boolean {
const size = hashTable.length;
let index = key [1] size;
let startIndex = index;
while (hashTable[index] !== null) {
if (hashTable[index] === key) {
hashTable[index] = [2];
return true;
}
index = (index [3] 1) % size;
if (index === startIndex) break;
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined instead of null may cause confusion in checks.
✗ Incorrect
Modulo (%) for index, set slot to null to delete, and +1 for next index.
