0
0
DSA Pythonprogramming~20 mins

Hash Table Concept and Hash Functions in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Hash Table Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Hash Table Insertions with Simple Hash Function
What is the printed state of the hash table after inserting keys 10, 22, 31 using the hash function key % 10?
DSA Python
class SimpleHashTable:
    def __init__(self):
        self.size = 10
        self.table = [[] for _ in range(self.size)]

    def hash_function(self, key):
        return key % self.size

    def insert(self, key):
        index = self.hash_function(key)
        self.table[index].append(key)

    def __str__(self):
        result = []
        for i, bucket in enumerate(self.table):
            if bucket:
                result.append(f"{i}: {bucket}")
        return ' | '.join(result)

ht = SimpleHashTable()
ht.insert(10)
ht.insert(22)
ht.insert(31)
print(str(ht))
A0: [10, 31] | 2: [22]
B0: [10] | 2: [22, 31]
C1: [10] | 2: [22] | 3: [31]
D0: [10] | 1: [31] | 2: [22]
Attempts:
2 left
💡 Hint
Remember the hash function is key % 10, so find the remainder when dividing each key by 10.
🧠 Conceptual
intermediate
1:30remaining
Understanding Hash Function Output Range
If a hash function is defined as h(key) = key % 7, what is the maximum number of buckets the hash table should have to avoid index errors?
A7
B6
C8
D10
Attempts:
2 left
💡 Hint
Think about the possible remainders when dividing by 7.
🔧 Debug
advanced
2:00remaining
Identify the Error in Hash Table Key Insertion
What error will this code produce when inserting keys into the hash table?
DSA Python
class HashTable:
    def __init__(self, size):
        self.size = size
        self.table = [None] * size

    def hash_function(self, key):
        return key % self.size

    def insert(self, key):
        index = self.hash_function(key)
        if self.table[index] is None:
            self.table[index] = key
        else:
            self.table[index].append(key)

ht = HashTable(5)
ht.insert(10)
ht.insert(15)
print(ht.table)
ANo error, prints [10, 15, None, None, None]
BIndexError: list index out of range
CAttributeError: 'int' object has no attribute 'append'
DTypeError: unsupported operand type(s) for %: 'str' and 'int'
Attempts:
2 left
💡 Hint
Check what type is stored in the table before calling append.
Predict Output
advanced
2:00remaining
Output of Hash Table with Linear Probing
What is the printed state of the hash table after inserting keys 5, 15, 25 using linear probing with size 10 and hash function key % 10?
DSA Python
class LinearProbingHashTable:
    def __init__(self):
        self.size = 10
        self.table = [None] * self.size

    def hash_function(self, key):
        return key % self.size

    def insert(self, key):
        index = self.hash_function(key)
        start_index = index
        while self.table[index] is not None:
            index = (index + 1) % self.size
            if index == start_index:
                raise Exception('Hash table is full')
        self.table[index] = key

    def __str__(self):
        return str(self.table)

ht = LinearProbingHashTable()
ht.insert(5)
ht.insert(15)
ht.insert(25)
print(ht)
A[5, None, None, None, None, 15, 25, None, None, None]
B[5, 15, 25, None, None, None, None, None, None, None]
C[5, None, None, None, None, None, None, 15, 25, None]
D[5, None, None, None, None, None, None, None, None, 15]
Attempts:
2 left
💡 Hint
Check the hash index for each key and how linear probing moves to next slot if occupied.
🧠 Conceptual
expert
1:30remaining
Choosing a Good Hash Function Property
Which property is most important for a hash function to minimize collisions in a hash table?
AFast computation time regardless of input size
BUniform distribution of hash values across buckets
CDeterministic output for the same input
DAbility to produce very large hash values
Attempts:
2 left
💡 Hint
Think about how keys are spread in the table to avoid clustering.