0
0
DSA Pythonprogramming~10 mins

Collision Handling Using Chaining in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Adict
Blist
Cset
Drange
Attempts:
3 left
💡 Hint
Common Mistakes
Using dict() or set() instead of range() causes errors.
Using list() creates an empty list, not a range.
2fill in blank
medium

Complete 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'
A%
B+
C//
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using + or * can produce indexes outside the table range.
Using // gives the quotient, not the remainder.
3fill in blank
hard

Fix 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'
Akey
Bvalue
Cindex
Dhash_table
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.
4fill in blank
hard

Fill 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'
A%
B+
Cindex
Dkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of % causes index errors.
Using key instead of index to access the bucket causes runtime errors.
5fill in blank
hard

Fill 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'
A%
B+
Ckey
Dindex
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.