Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the hash index for a given key.
DSA Python
def hash_function(key, size): return key [1] size
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using + or - instead of modulo causes index out of range errors.
Using * can produce very large numbers not suitable for indexing.
✗ Incorrect
The modulo operator (%) is used to ensure the hash index stays within the table size.
2fill in blank
mediumComplete the code to find the next index in linear probing.
DSA Python
def linear_probe(index, size): return (index [1] 1) % size
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction (-) moves backward, which is incorrect for linear probing.
Using multiplication (*) or floor division (//) does not move to the next slot.
✗ Incorrect
In linear probing, we move 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 Python
def insert(hash_table, key, size): index = hash_function(key, size) start_index = index while hash_table[index] is not None: index = (index [1] 1) % size if index == start_index: return False # Table full hash_table[index] = key return True
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction (-) can cause infinite loops or wrong slot checks.
Using multiplication or floor division is incorrect for index increment.
✗ Incorrect
We add 1 to the index to move to the next slot in linear probing.
4fill in blank
hardFill both blanks to complete the search function using linear probing.
DSA Python
def search(hash_table, key, size): index = hash_function(key, size) start_index = index while hash_table[index] is not None and hash_table[index] != [1]: index = (index [2] 1) % size if index == start_index: return -1 # Not found if hash_table[index] == key: return index return -1
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with size instead of key causes wrong search results.
Using subtraction (-) instead of addition (+) moves backward incorrectly.
✗ Incorrect
We compare the slot with the key and move forward by adding 1 to index.
5fill in blank
hardFill all three blanks to complete the delete function using linear probing.
DSA Python
def delete(hash_table, key, size): index = hash_function(key, size) start_index = index while hash_table[index] is not None: if hash_table[index] == [1]: hash_table[index] = [2] # Mark as deleted return True index = (index [3] 1) % size if index == start_index: return False # Not found return False
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the slot to None prevents searches from probing past the deleted slot, causing items to become unreachable.
Using subtraction (-) instead of addition (+) moves backward incorrectly.
✗ Incorrect
We check if the slot has the key, mark it as 0 (deleted marker) to delete, and move forward by adding 1.