195. Design HashMap

Easy · Design

Design a HashMap class that supports the following operations: put(key, value) to insert or update a key-value pair, get(key) to retrieve the value associated with a key (or -1 if not found), and remove(key) to delete a key-value pair.

You must implement the HashMap from scratch without using any built-in hash table libraries. Your implementation should handle collisions and support integer keys and values.

Examples

Example 1
Input: Operations: [["put", 1, 1], ["put", 2, 2], ["get", 1], ["get", 3], ["put", 2, 1], ["get", 2], ["remove", 2], ["get", 2]]
Output: [null, null, 1, -1, null, 1, null, -1]
Explanation: After put(1,1) and put(2,2), get(1) returns 1. get(3) returns -1 (key not found). After updating put(2,1), get(2) returns 1. After remove(2), get(2) returns -1.
Example 2
Input: Operations: [["put", 0, 0], ["get", 0], ["remove", 0], ["get", 0]]
Output: [null, 0, null, -1]
Explanation: Simple sequence: insert 0→0, retrieve it, remove it, then it's gone.

Constraints