← 返回 amazon 的题目列表Implement a HashMap Without Built-in Libraries
类型:online_judge
Problem: Implement a HashMap (No Built-in Hash Containers)
Design and implement a simplified HashMap data structure. You must not use any built-in hash containers/libraries (e.g., Python dict/set, Java HashMap, C++ unordered_map).
Support the following operations:
put(key, value): If key exists, update its value; otherwise insert the pair.
get(key) -> value: Return the value for key, or -1 if absent.
remove(key): Remove the pair if key exists.
I/O Convention (Interview Style)
Given two inputs:
An array of operation names ops (e.g., put get remove get)
An array args where each operation has its arguments:
put has two integers: key value
get/remove has one integer: key
Output one line per get operation.
Constraints
0 <= key <= 10^6
0 <= value <= 10^6
Number of operations n <= 2 * 10^5
Target average time complexity: O(1) (use arrays + chaining/open addressing for collisions)
Test Cases
(See the Chinese version for 5 concrete cases; same cases apply.)
Example
Input
put put get get remove get
1 1
2 2
1
3
2
2
Output
1
-1
-1