0
0
DSA Pythonprogramming~10 mins

HashMap Implementation from Scratch in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - HashMap Implementation from Scratch
Start
Compute hash(key)
Find bucket index = hash % capacity
Check bucket for key
Key found?
Update value
Done
Add new key-value pair
Done
The flow shows how a key is hashed to find a bucket, then the bucket is checked for the key. If found, update value; if not, add new pair.
Execution Sample
DSA Python
class HashMap:
    def __init__(self):
        self.capacity = 4
        self.buckets = [[] for _ in range(self.capacity)]

    def put(self, key, value):
        index = hash(key) % self.capacity
        bucket = self.buckets[index]
        for i, (k, v) in enumerate(bucket):
            if k == key:
                bucket[i] = (key, value)
                return
        bucket.append((key, value))
This code initializes a hashmap with 4 buckets and starts the put operation by computing the bucket index.
Execution Table
StepOperationKeyValueBucket IndexBucket BeforeBucket AfterVisual State
1Initialize HashMap---[][][[], [], [], []]
2put('apple', 10)apple103[][('apple', 10)][[], [], [], [('apple', 10)]]
3put('banana', 20)banana200[][('banana', 20)][[('banana', 20)], [], [], [('apple', 10)]]
4put('apple', 15) updateapple153[('apple', 10)][('apple', 15)][[('banana', 20)], [], [], [('apple', 15)]]
5put('grape', 30)grape302[][('grape', 30)][[('banana', 20)], [], [('grape', 30)], [('apple', 15)]]
6put('melon', 40)melon401[][('melon', 40)][[('banana', 20)], [('melon', 40)], [('grape', 30)], [('apple', 15)]]
7put('banana', 25) updatebanana250[('banana', 20)][('banana', 25)][[('banana', 25)], [('melon', 40)], [('grape', 30)], [('apple', 15)]]
8put('kiwi', 50)kiwi503[('apple', 15)][('apple', 15), ('kiwi', 50)][[('banana', 25)], [('melon', 40)], [('grape', 30)], [('apple', 15), ('kiwi', 50)]]
9End of operations-----[[('banana', 25)], [('melon', 40)], [('grape', 30)], [('apple', 15), ('kiwi', 50)]]
💡 All put operations completed, hashmap buckets updated accordingly.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8Final
buckets[[], [], [], []][[], [], [], [('apple', 10)]][[('banana', 20)], [], [], [('apple', 10)]][[('banana', 20)], [], [], [('apple', 15)]][[('banana', 20)], [], [('grape', 30)], [('apple', 15)]][[('banana', 20)], [('melon', 40)], [('grape', 30)], [('apple', 15)]][[('banana', 25)], [('melon', 40)], [('grape', 30)], [('apple', 15)]][[('banana', 25)], [('melon', 40)], [('grape', 30)], [('apple', 15), ('kiwi', 50)]][[('banana', 25)], [('melon', 40)], [('grape', 30)], [('apple', 15), ('kiwi', 50)]]
Key Moments - 3 Insights
Why do we use hash(key) % capacity to find the bucket index?
Because hash(key) can be a very large number, using modulo with capacity ensures the index fits within the fixed number of buckets, as shown in steps 2-8 in the execution_table.
What happens if the key already exists in the bucket?
We find the key in the bucket and update its value instead of adding a new pair, as seen in steps 4 and 7 where 'apple' and 'banana' values are updated.
Why do buckets hold lists of key-value pairs?
Because multiple keys can hash to the same bucket index (collision), so we store pairs in a list to handle collisions, demonstrated by step 8 where 'apple' and 'kiwi' share bucket 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What is the bucket index for key 'apple'?
A1
B3
C0
D2
💡 Hint
Check the 'Bucket Index' column at step 4 in the execution_table.
At which step does the bucket for index 0 get updated with a new value for 'banana'?
AStep 2
BStep 3
CStep 7
DStep 8
💡 Hint
Look for 'banana' updates in the 'Operation' column and bucket changes at index 0.
If we add a new key 'pear' that hashes to bucket 2, what will happen to the bucket at index 2?
AIt will add ('pear', value) to the list
BIt will replace existing pairs
CIt will clear the bucket
DIt will cause an error
💡 Hint
Buckets hold lists of pairs to handle collisions, as shown in step 8 where multiple pairs exist in one bucket.
Concept Snapshot
HashMap stores key-value pairs in buckets.
Hash function maps key to bucket index using modulo.
Each bucket holds a list to handle collisions.
put(key, value) adds or updates pairs in the bucket.
Lookup uses same hash and bucket to find value.
Simple, fast average access by key.
Full Transcript
This visual execution traces a simple HashMap implementation from scratch. The HashMap has a fixed capacity of 4 buckets, each a list to hold key-value pairs. When putting a key-value pair, the key is hashed and modulo applied to find the bucket index. The bucket is checked for the key; if found, the value is updated, else the pair is added. The execution table shows step-by-step how keys like 'apple', 'banana', 'grape', 'melon', and 'kiwi' are added or updated in their respective buckets. The variable tracker shows the bucket states after each operation. Key moments clarify why modulo is used, how updates happen, and why buckets hold lists. The visual quiz tests understanding of bucket indexing, updates, and collision handling. The concept snapshot summarizes the core ideas of HashMap storage and operations.