Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the hash table with empty lists for chaining.
DSA Python
hash_table = [[] for _ in [1](10)]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dict() or set() instead of range() causes errors.
Using list() creates an empty list, not a range.
✗ Incorrect
We use range(10) to create 10 empty lists for chaining in the hash table.
2fill in blank
mediumComplete the code to compute the hash index for a given key.
DSA Python
index = hash(key) [1] len(hash_table)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using + or * can produce indexes outside the table range.
Using // gives the quotient, not the remainder.
✗ Incorrect
We use modulo (%) to ensure the index fits within the hash table size.
3fill in blank
hardFix the error in the code that inserts a key-value pair into the hash table using chaining.
DSA Python
def insert(hash_table, key, value): index = hash(key) % len(hash_table) for i, (k, v) in enumerate(hash_table[index]): if k == key: hash_table[index][i] = (key, [1]) return hash_table[index].append((key, value))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Replacing with key instead of value loses the actual data.
Using index or hash_table as value causes errors.
✗ Incorrect
We update the value for the existing key, so we replace with (key, value).
4fill in blank
hardFill both blanks to complete the search function that returns the value for a given key or None if not found.
DSA Python
def search(hash_table, key): index = hash(key) [1] len(hash_table) for k, v in hash_table[[2]]: if k == key: return v return None
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of % causes index errors.
Using key instead of index to access the bucket causes runtime errors.
✗ Incorrect
We use modulo (%) to get the index, then look in hash_table[index] for the key.
5fill in blank
hardFill all three blanks to complete the delete function that removes a key-value pair from the hash table.
DSA Python
def delete(hash_table, key): index = hash(key) [1] len(hash_table) for i, (k, v) in enumerate(hash_table[[2]]): if k == [3]: del hash_table[index][i] return True return False
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of % causes wrong index.
Using key instead of index to access bucket causes errors.
Comparing k to index instead of key causes logic errors.
✗ Incorrect
We compute index with %, access bucket with index, and compare k to key to delete.