0
0
DSA Pythonprogramming~10 mins

Collision Handling Using Open Addressing Linear Probing 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 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'
A+
B-
C*
D%
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.
2fill in blank
medium

Complete 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'
A+
B-
C*
D//
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.
3fill in blank
hard

Fix 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'
A*
B-
C+
D//
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.
4fill in blank
hard

Fill 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'
Akey
Bsize
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with size instead of key causes wrong search results.
Using subtraction (-) instead of addition (+) moves backward incorrectly.
5fill in blank
hard

Fill 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'
Akey
BNone
C+
D-
E0
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.