EasyArrayHash TableLinked ListDesignHash FunctionUpdated Sep 2026

706. Design HashMap

Asked at Goldman Sachs

Difficulty

Easy

Time

O(1) average per operation, O(n

Space

O(size + n)

Pattern

Array

Companies

1

Problem

Build a simple hash map class that stores integer keys and integer values. You may not use any built-in hash table or dictionary — you must implement the storage yourself.

Your class needs three methods. put(key, value) stores the pair. If the key is already in the map, replace its old value with the new one (do not add a second copy). get(key) returns the value stored for that key, or -1 if the key is not in the map. remove(key) deletes the key and its value; if the key is not there, do nothing.

The real point of this problem is showing that you know what happens inside a hash map: a fixed array of buckets, a hash function that turns a key into a bucket index, and a way to handle collisions (two keys landing in the same bucket). A common choice is bucket = key % size with a prime size, and each bucket holds a small list of (key, value) pairs.

What the map must do

put(1, 1)put(2, 2)put(2, 1) overwritesremove(2)The sequence of calls from the example, in ordermapkeyvaluekeykey1121After put(2,1): key 2 keeps one entry, its value isnow 1
Keys map to values, and the same key can only appear once — a second put replaces the value.

Constraints

  • ●0 <= key, value <= 10^6
  • ●At most 10^4 calls will be made to put, get, and remove

Examples

Example 1
Input["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"] [[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
Output[null, null, null, 1, -1, null, 1, null, -1]
WhyAfter putting (1,1) and (2,2), get(1) finds 1 and get(3) finds nothing so it returns -1. The second put(2,1) overwrites the old value, so get(2) now returns 1. remove(2) deletes that key, so the final get(2) returns -1.
Example 2
Input["MyHashMap", "put", "get", "remove", "get"] [[], [5, 10], [5], [5], [5]]
Output[null, null, 10, null, -1]
WhyWe store 5 → 10, read it back as 10, then remove key 5. After removal the key is gone, so get(5) returns -1.

Asked At

1 companies
CompanyDifficulty
Goldman SachsEasyView all Goldman Sachs questions →

How to Think About It

1

Direct addressing (an array of size 10^6 + 1) technically works for the given key range but misses the point of the exercise.

2

Key insight: use a fixed array of buckets, compute bucket = key % size, and store (key, value) pairs in each bucket so collisions are handled by chaining.

3

put: scan the bucket; update the value if the key exists, otherwise append a new pair. get: scan and return the value or -1. remove: scan and delete the pair if found.

4

Choose a prime bucket count (for example 1009 or 2069) to spread keys that share common factors.

5

Follow-up talking points: load factor, resizing when buckets get long, and open addressing as an alternative to chaining.

Optimal Approach

State: buckets, an array of size lists.

put(key, value): find the bucket key % size; if a pair with key exists, overwrite its value; otherwise append (key, value).
get(key): scan the bucket; return the value if found, else -1.
remove(key): scan the bucket; delete the pair with key if present.

Time: O(n / size) average per operation, O(1) with a reasonable load factor. Space: O(size + n).

Bucket array with chaining

bucketspairs0123—(1,1)—(2,2)key % 4 sends key 1 to bucket 1 and key 2 to bucket 2bucket = key % sizeput: update if key found, else appendget: scan bucket, else return -1remove: scan bucket, delete the pair
The key is hashed to a bucket index; each bucket is a short list of pairs, so collisions just sit side by side.
ComplexityTime O(1) average per operation, O(n Space O(size + n)Each operation scans only one bucket, so the cost is the average bucket length; the array of buckets plus the stored pairs take the space.

What Trips People Up in Real Interviews

1

Using the language's built-in dictionary inside your class. That defeats the purpose of the question.

2

Appending a duplicate pair on put instead of updating the existing key.

3

Returning 0 or null from get for a missing key instead of -1.

4

Not discussing collisions or load factor. That discussion is usually worth more than the code itself.

Solution Code

O(1) average per operation, O(n time
class MyHashMap:
    def __init__(self):
        self.size = 2069
        self.buckets = [[] for _ in range(self.size)]

    def put(self, key, value):
        bucket = self.buckets[key % self.size]
        for pair in bucket:
            if pair[0] == key:
                pair[1] = value
                return
        bucket.append([key, value])

    def get(self, key):
        for k, v in self.buckets[key % self.size]:
            if k == key:
                return v
        return -1

    def remove(self, key):
        bucket = self.buckets[key % self.size]
        for i, pair in enumerate(bucket):
            if pair[0] == key:
                bucket.pop(i)
                return

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Design HashMap problem?

Design HashMap asks you to build `put`, `get`, and `remove` for integer keys without using a built-in hash table. The interviewer is checking that you understand how hash maps work inside: a bucket array, a hash function, and collision handling.

How do you solve Design HashMap?

The optimal approach runs in O(1) average per operation, O(n time and O(size + n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Design HashMap?

Design HashMap is asked at Goldman Sachs. It is a easy difficulty problem.

What are common mistakes on Design HashMap?
  • Using the language's built-in dictionary inside your class. That defeats the purpose of the question.
  • Appending a duplicate pair on `put` instead of updating the existing key.
  • Returning `0` or `null` from `get` for a missing key instead of `-1`.
  • Not discussing collisions or load factor. That discussion is usually worth more than the code itself.