← 返回 goldmansachs 的题目列表Design HashMap
类型:online_judge
Problem: Design HashMap (without built-in hash table)
Design and implement a HashMap that supports:
put(key, value): Insert the mapping from key to value. If key already exists, update its value.
get(key): Return the value associated with key; return -1 if not found.
remove(key): Remove the mapping for key if it exists.
Requirements
You are not allowed to use the language's built-in hash table/dictionary (e.g., Python dict, Java HashMap) as the primary storage.
You must handle hash collisions yourself (e.g., separate chaining or open addressing).
Constraints (typical interview setting)
0 <= key <= 10^6
0 <= value <= 10^6
Total number of put/get/remove calls is at most 2 * 10^4
Example
Sequence of operations:
put(1, 1)
put(2, 2)
get(1) → 1
get(3) → -1
put(2, 1)
get(2) → 1
remove(2)
get(2) → -1
Example
Input
put 1 1
put 2 2
get 1
get 3
put 2 1
get 2
remove 2
get 2
Output
1
-1
1
-1