Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the hash index for a given key.
DSA C#
int Hash(int key) {
return key [1] size;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) instead of modulo (%) causes index out of range.
Using multiplication (*) or addition (+) changes the hash value incorrectly.
✗ Incorrect
The modulo operator (%) is used to ensure the hash index stays within the array size.
2fill in blank
mediumComplete the code to find the next index using linear probing.
DSA C#
int NextIndex(int currentIndex) {
return (currentIndex [1] 1) % size;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction (-) causes backward movement.
Using multiplication (*) or division (/) is incorrect for index increment.
✗ Incorrect
Adding 1 moves to the next slot in linear probing, and modulo wraps around.
3fill in blank
hardFix the error in the insertion loop to handle collisions correctly.
DSA C#
while (table[index] != null) { index = (index [1] 1) % size; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction (-) causes infinite loop or wrong index.
Using multiplication (*) or division (/) is invalid for index increment.
✗ Incorrect
Incrementing index by 1 with modulo handles linear probing correctly.
4fill in blank
hardFill both blanks to complete the search method using linear probing.
DSA C#
int Search(int key) {
int index = Hash(key);
while (table[index] != null && table[index].Key != key) {
index = (index [1] 1) [2] size;
}
return (table[index] != null) ? index : -1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction (-) causes wrong index movement.
Using multiplication (*) is invalid for index calculation.
✗ Incorrect
Add 1 to index and use modulo (%) to wrap around the table during search.
5fill in blank
hardFill all three blanks to complete the Remove method using linear probing.
DSA C#
bool Remove(int key) {
int index = Hash(key);
while (table[index] != null) {
if (table[index].Key == key) {
table[index] = null; // Remove entry
index = (index [1] 1) [2] size;
while (table[index] != null) {
var temp = table[index];
table[index] = null;
Insert(temp.Key, temp.Value);
index = (index [3] 1) % size;
}
return true;
}
index = (index + 1) % size;
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction (-) causes wrong index movement.
Using multiplication (*) is invalid for index calculation.
✗ Incorrect
Use addition (+) to move forward and modulo (%) to wrap index during rehashing after removal.
