Design a hash map that supports insertion, lookup, and deletion of integer key-value pairs.
Problem
Implement a hash map from scratch with the following operations:
put(key, value): Insert the pair(key, value)into the map. Ifkeyalready exists, update its value.get(key): Return the value associated withkey, or-1if the key does not exist.remove(key): Delete the key and its value from the map if it exists.
Your implementation should behave like a simple associative array for integer keys and values.
Goal
Design the data structure so that the operations are efficient on average and handle key collisions correctly.
Input Format
The interface is called directly by the judge.
put(key, value)get(key)remove(key)
All keys and values are integers.
Output Format
get(key)returns the stored value, or-1if the key is absent.putandremovedo not return a value.
Constraints
- Keys and values are integers.
- The number of operations can be large, so average-case efficiency matters.
- The structure must support updates and deletions.
Note: Exact official constraints are not provided here; use a standard hash map design approach.
Example 1
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
Explanation
get(1)returns1.get(3)returns-1because the key is missing.put(2, 1)updates the existing value for key2.get(2)returns1.- After
remove(2),get(2)returns-1.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.