Skip to content

Practice Design HashMap

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#706
LeetCode Problem

Design HashMap

Problem Statement

Implement MyHashMap without a built-in hash-table library. The constructor starts empty. put(key, value) inserts a mapping or replaces its value. get(key) returns the mapped value or -1 when absent. remove(key) deletes the mapping if present. Practice approach: The bounded nonnegative key range permits a direct-address array initialized with -1. Values are nonnegative, so -1 is a safe absence marker. A bucket array with collision chains is a more general alternative. Test overwriting a key, removing it twice, and retrieving a missing key.

Source: Design HashMap on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["MyHashMap","put","put","get","put","get","remove","get"]
[[],[4,8],[7,2],[4],[4,9],[4],[4],[4]]
Output
[null,null,null,8,null,9,null,-1]
Explanation

The second put for key 4 replaces 8 with 9. After removal, get(4) reports absence.

Constraints

0 <= key, value <= 10^6
At most 10^4 total calls to put, get, and remove.

Related LeetCode Problems

Practice more problems similar to Design HashMap.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.