A school clerk wants a fast way to find a student's marks from the roll number. Build this lookup table yourself. It is a hash map.
Do not use a ready-made hash table, such as the dictionary, map or object type of your language.
Write a class MyHashMap with these methods:
MyHashMap(): make an empty map.
put(key, value): save the pair. If key is already saved, replace its old value with the new one.
get(key): return the value saved for key. Return -1 if key is not saved.
remove(key): forget key and its value. If key is not saved, do nothing.
Examples
Example 1:
Input
["MyHashMap", "put", "put", "get", "put", "get", "remove", "get", "remove", "get", "get"]
[[], [101, 35], [202, 48], [101], [101, 40], [101], [202], [202], [999], [999], [101]]
Output
[null, null, null, 35, null, 40, null, -1, null, -1, 40]
Explanation
put(101, 35) and put(202, 48) save two pairs. get(101) returns 35.
put(101, 40) replaces the value of key 101, so the next get(101) returns 40.
remove(202) forgets key 202, so get(202) returns -1.
remove(999) does nothing, because 999 was never saved. get(999) returns -1.
The last get(101) still returns 40.
Example 2:
Input
["MyHashMap", "get", "put", "get", "remove", "get"]
[[], [0], [0, 0], [0], [0], [0]]
Output
[null, -1, null, 0, null, -1]
Explanation
Key 0 is not saved at first, so get(0) returns -1.
After put(0, 0), get(0) returns the saved value 0. This is not the same as "missing".
After remove(0), get(0) returns -1 again.