Design HashMap
Build a key-value map from an array of buckets, without a built-in dict.
The problem
Design a hash map from integer keys to integer values without using Python's built-in dict to store them. Implement the MyHashMap class: MyHashMap() creates an empty map; put(key, value) stores the pair, and if key is already in the map it replaces the old value; get(key) returns the value for key, or -1 if key is not in the map; remove(key) deletes key and its value, and does nothing if key is not in the map. Keys and values are between 0 and 1,000,000, and there are at most 10,000 calls in total.
Starter code
class MyHashMap:
def __init__(self):
passPublic examples
obj = MyHashMap() obj.put(1, 1) obj.put(2, 2) obj.get(1) obj.get(3) obj.put(2, 1) obj.get(2) obj.remove(2) obj.get(2)gives[None, None, None, 1, -1, None, 1, None, -1]
Solving it in the practice book needs an active paid plan.
Open it in the practice bookOne thought at a time.
