Implement an Integer Hash Map
Company: Goldman Sachs
Role: Software Engineer
Category: Coding & Algorithms
Interview Round: Take-home Project
Quick Answer: This question evaluates understanding of hashing principles, collision handling, and data structure design for integer key-value storage, testing algorithmic complexity and API correctness within the Coding & Algorithms domain.
Constraints
- 1 <= len(operations) <= 100000
- operations[0] == 'MyHashMap'
- 0 <= key, value <= 10^9
- Do not use built-in map, dictionary, or hash table implementations
- Aim for average O(1) time per operation
Examples
Input: (['MyHashMap', 'put', 'put', 'get', 'get', 'put', 'get', 'remove', 'get'], [[], [1, 10], [2, 20], [1], [3], [2, 30], [2], [2], [2]])
Expected Output: [None, None, None, 10, -1, None, 30, None, -1]
Explanation: Insert two keys, read an existing key and a missing key, update key 2, then remove it.
Input: (['MyHashMap', 'get', 'remove', 'put', 'get'], [[], [42], [42], [42, 0], [42]])
Expected Output: [None, -1, None, None, 0]
Explanation: Getting or removing a missing key should not fail. After inserting key 42 with value 0, `get(42)` returns 0.
Input: (['MyHashMap', 'put', 'put', 'put', 'get', 'remove', 'get'], [[], [0, 0], [0, 5], [0, 7], [0], [0], [0]])
Expected Output: [None, None, None, None, 7, None, -1]
Explanation: The same key is updated multiple times. The latest value is returned, and after removal the key is missing.
Input: (['MyHashMap', 'put', 'put', 'get', 'get', 'remove', 'get', 'get'], [[], [1, 100], [2070, 200], [1], [2070], [1], [1], [2070]])
Expected Output: [None, None, None, 100, 200, None, -1, 200]
Explanation: These keys collide under a common modulo-based hash setup, so this checks collision handling with separate chaining.
Hints
- Map each key to a bucket index using a hash function such as `key % bucket_count`.
- If multiple keys land in the same bucket, store all pairs in that bucket and scan only that small bucket to find or update a key.