Design a HashMap without using any built-in hash table libraries. Implement a MyHashMap class with methods to add (or update) a key-value pair, retrieve a value by key, and remove a mapping by key.
Key Insights
Use a fixed-size array of buckets where each bucket uses a linked list to handle collisions.
Choose a simple hash function (e.g., modulo operation) to map a key to its corresponding bucket.
For each bucket, traverse the linked list to update an existing key or append a new node if the key does not exist.
Removal requires updating pointers in the linked list to bypass the removed node.
Space and Time Complexity
Time Complexity: Average O(1) for put, get, and remove; worst-case O(n) when many keys hash to the same bucket.
Space Complexity: O(n), where n is the number of key-value pairs stored.
Solution
We implement the HashMap using an array of fixed size (e.g., 1000). Each array element is the head of a linked list that manages collisions using chaining. The key is hashed using a modulo operation.
For the put operation, if a key already exists in the chain, its value is updated; otherwise, a new node is appended.
The get operation traverses the chain at the computed bucket index to find and return the corresponding value, or returns -1 if not found.
The remove operation also traverses the list, carefully reconnecting the links to remove the target node while handling edge cases such as removal at the head.
Code Solutions
# ListNode class for handling collisions in each bucketclassListNode:def__init__(self, key, value,next=None): self.key = key # Store key self.value = value # Store value self.next=next# Pointer to the next nodeclassMyHashMap:def__init__(self): self.size =1000# Define the bucket size self.buckets =[None]* self.size # Initialize buckets as an array of Nonedefhash(self, key):# Simple modulo based hash function to map keys to bucket indicesreturn key % self.size
defput(self, key:int, value:int)->None: index = self.hash(key)if self.buckets[index]isNone:# If the bucket is empty, create a new node and place it here self.buckets[index]= ListNode(key, value)else: current = self.buckets[index]whileTrue:if current.key == key:# If key exists, update its value current.value = value
returnif current.nextisNone:break current = current.next# Append a new node at the end of the list if key not found current.next= ListNode(key, value)defget(self, key:int)->int: index = self.hash(key) current = self.buckets[index]while current:if current.key == key:# Key found; return its valuereturn current.value
current = current.next# Key not found; return -1return-1defremove(self, key:int)->None: index = self.hash(key) current = self.buckets[index]if current isNone:returnif current.key == key:# Remove head node if it contains the key self.buckets[index]= current.nextelse: prev = current
current = current.nextwhile current:if current.key == key:# Bypass the node to remove it from the list prev.next= current.nextreturn prev, current = current, current.next