This question evaluates understanding of hashing principles, collision handling, and data structure design for integer key-value storage, testing algorithmic complexity and API correctness within the Coding & Algorithms domain.
Design a data structure MyHashMap that stores integer keys and integer values without using any built-in hash table library.
Implement the following operations:
put(key, value)
: Insert the key-value pair into the map. If the key already exists, update its value.
get(key)
: Return the value associated with the key. If the key does not exist, return
-1
.
remove(key)
: Remove the key and its value if the key exists.
Requirements:
Example:
put(1, 10)
put(2, 20)
get(1)
returns
10
get(3)
returns
-1
put(2, 30)
get(2)
returns
30
remove(2)
get(2)
returns
-1