Design HashMap

EasyDesign · Hashing · Separate Chaining🧩 Hashing & CountingLeetCode #706 ↗Updated

Problem

Implement your own MyHashMap for integer keys and values without any built-in hash table. put(key, value) inserts or overwrites, get(key) returns the stored value or -1 if absent, and remove(key) deletes the key if it exists.
Inputput(1,10) put(2,20) get(1) get(3)Output10, -1
Inputput(2,5) get(2)Output5 (overwritten)
Inputremove(2) get(2)Output-1

0 ≤ key, value ≤ 106  ·  at most 104 calls in total  ·  no built-in hash map / set

Try it yourself

Java
class MyHashMap {
    public MyHashMap() {
        
    }

    public void put(int key, int value) {
        
    }

    public int get(int key) {
        
    }

    public void remove(int key) {
        
    }
}
Your code is saved in this browser.Submit on LeetCode ↗
Key insight
Hash the key to pick one of a small number of buckets, and keep a short list of entries in each bucket. With a good spread and resizing at a load factor, every list stays O(1) long on average.
PatternHashing & Counting

Instinct Need "have I seen this before?", "how many times?" or "group things that look alike"? Trade memory for O(1) lookups with a HashMap, HashSet or int[26].

Memory hack Don't search, remember - every element checks in at the front desk, and the desk answers instantly.

Open the Hashing & Counting guide →

Approaches

Brute ForceUnsorted List of Pairs

Time O(n) per opSpace O(n)

Store every (key, value) pair in an ArrayList. Each operation scans the list for the key: overwrite or append on put, return or -1 on get, delete on remove.

Java
class MyHashMap {
    private final List<int[]> entries = new ArrayList<>(); // each = {key, value}
    public MyHashMap() {}
    private int find(int key) {
        for (int i = 0; i < entries.size(); i++) if (entries.get(i)[0] == key) return i;
        return -1;
    }
    public void put(int key, int value) {
        int i = find(key);
        if (i >= 0) entries.get(i)[1] = value;
        else entries.add(new int[]{key, value});
    }
    public int get(int key) {
        int i = find(key);
        return i >= 0 ? entries.get(i)[1] : -1;
    }
    public void remove(int key) {
        int i = find(key);
        if (i >= 0) entries.remove(i);
    }
}

Correct and tiny, but every call is a linear scan — not a hash map at all.

At a glance

LevelApproachTimeSpace
Brute ForceUnsorted List of PairsO(n) per opO(n)
BetterDirect-Address ArrayO(1) per opO(U), U = 106+1
OptimalSeparate Chaining with ResizingO(1) average per opO(n)
Common pitfalls
  • put on an existing key appending a second entry instead of overwriting.
  • Losing the rest of a chain when removing its head node.
  • Negative indexes from hashCode() % cap — mask or use Math.floorMod.
Interview tips
  • Clarify key range and call count first — it justifies or rules out direct addressing.
  • Talk about load factor and resizing; a fixed bucket count degrades to O(n).
  • Compare chaining with open addressing and their trade-offs.
Pattern
Hash table design → buckets + collision handling whenever you must build O(1) lookup yourself.
My notes saved in this browser