Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a hash table using an array of linked lists.
DSA C++
std::list<int> hashTable[[1]];Attempts:
3 left
💡 Hint
Common Mistakes
Using a type name instead of a number for array size.
Using a function name instead of a number.
✗ Incorrect
The hash table size is typically a fixed integer representing the number of buckets. Here, 10 buckets are declared as an array of lists.
2fill in blank
mediumComplete the code to compute the hash index for a key using modulo operator.
DSA C++
int index = key [1] tableSize;Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) instead of modulo.
Using multiplication (*) or addition (+) which do not limit the index.
✗ Incorrect
The modulo operator (%) is used to wrap the key into the range of the table size, giving a valid bucket index.
3fill in blank
hardFix the error in the hash function to correctly return the index.
DSA C++
int hashFunction(int key, int tableSize) {
return key [1] tableSize;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) which returns a quotient, not an index.
Using addition (+) or subtraction (-) which can exceed table size.
✗ Incorrect
The hash function must use modulo (%) to ensure the index is within the table size range.
4fill in blank
hardFill both blanks to complete the insertion function for the hash table.
DSA C++
void insert(int key, std::list<int> hashTable[], int tableSize) {
int index = key [1] tableSize;
hashTable[index].[2](key);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using addition (+) instead of modulo for index.
Using insert which requires an iterator, instead of push_back.
✗ Incorrect
The index is computed using modulo (%), and the key is added to the list using push_back to insert at the end.
5fill in blank
hardFill all three blanks to complete the search function in the hash table.
DSA C++
bool search(int key, std::list<int> hashTable[], int tableSize) {
int index = key [1] tableSize;
for (int val : hashTable[index]) {
if (val [2] key) {
return [3];
}
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == in the if condition.
Returning false inside the loop instead of true when found.
✗ Incorrect
The index is found using modulo (%). The loop checks if any value equals (==) the key. If found, return true.
