← Questions
Design a hash map without built-in libraries
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's technical phone screen, last asked Nov 2025. It tests hashing and data structures.
Implement a hash map from scratch, without any built-in hash table, dictionary, map or set. Keys and values are integers. You may use plain arrays and your own node classes.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
MyHashMap()— an empty map. -
put(key: int, value: int) -> None— inserts the key, or updates its value if present. -
get(key: int) -> int— the stored value, or -1 if the key is absent. -
remove(key: int) -> None— deletes the key if present.
Example
m = MyHashMap()
m.put(1, 10)
m.put(2, 20)
m.get(1) -> 10
m.put(2, 25) # update
m.get(2) -> 25
m.remove(1)
m.get(1) -> -1
# with 8 buckets and hash(k) = k mod 8, keys 1 and 9 share a bucket
m.put(1, 7); m.put(9, 3)
m.get(9) -> 3
m.remove(1)
m.get(9) -> 3 # removing one key must not lose the other
Constraints
- Keys and values are signed 32-bit integers.
- Up to 2 · 10^5 operations.
Clarifying questions
- Integer keys only, or should the design be generic?
- What should
getreturn for a missing key? (-1.) - Does it need to be thread-safe?
What a strong answer covers
- An array of buckets with separate chaining (a linked list per bucket), or open addressing with linear probing and tombstones for removal.
- A hash function that spreads keys well, handles negative keys, and maps to a bucket with a power-of-two mask or modulo.
- Resizing: track the count, double the bucket array when the load factor passes about 0.75, and rehash every entry; O(1) amortized per operation.
- Correct update and remove when keys collide.
- Tests: missing key, update, remove then reinsert, colliding keys, many inserts that trigger several resizes.
Follow-ups
- Shrink the table when it becomes mostly empty.
- Support any hashable key type.
- Make it safe for concurrent readers and writers.
Where it's asked
- Amazon · Technical phone screen · Last asked Nov 202560 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI
What it tests
hashing · data structures
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
