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, int size) {
return key [1] size;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication (*) instead of modulo (%) causes index out of bounds.
Using addition (+) or subtraction (-) does not limit the index properly.
✗ 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 next_index(int current, int size) {
return (current [1] 1) % size;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting 1 causes backward probing which is not standard.
Multiplying or dividing the index is incorrect for linear probing.
✗ Incorrect
Linear probing moves to the next slot by adding 1 to the current index.
3fill in blank
hardFix the error in the insertion loop to handle collisions correctly.
DSA C
while (table[index] != -1) { index = (index [1] 1) % size; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction causes infinite loops or wrong probing.
Multiplication or division is not suitable for linear probing.
✗ Incorrect
To resolve collisions, we move forward by adding 1 to the index and wrap around.
4fill in blank
hardFill both blanks to complete the search function using linear probing.
DSA C
int search(int table[], int size, int key) {
int index = key [1] size;
int start = index;
while (table[index] != key) {
index = (index [2] 1) % size;
if (index == start) return -1;
}
return index;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction for probing causes wrong search path.
Using multiplication or division breaks index calculation.
✗ Incorrect
Hash index is calculated using modulo (%), and linear probing moves forward by adding 1 (+).
5fill in blank
hardFill both blanks to complete the insertion function with linear probing collision handling.
DSA C
void insert(int table[], int size, int key) {
int index = key [1] size;
while (table[index] != -1) {
index = (index [2] 1) % size;
}
table[index] = key;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication in index calculation.
Adding operators when assigning key causes syntax errors.
✗ Incorrect
Index is calculated with modulo (%), linear probing adds 1 (+), and key is assigned directly without extra operators.
