Skip to content
Hello Python

Checking your account…

Sign in to save your code and progress across devices. The lesson and problem statement remain public.

Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace

Problem

Implement MyHashMap.put(key, value), get(key), and remove(key) without using Python dict as the key-value store. get returns -1 for a missing key.

Starter code

class MyHashMap:
    def __init__(self):
        pass
Test cases

replace-remove

{
  "operations": [
    "MyHashMap",
    "put",
    "put",
    "get",
    "get",
    "put",
    "get",
    "remove",
    "get"
  ],
  "arguments": [
    [],
    [
      1,
      1
    ],
    [
      2,
      2
    ],
    [
      1
    ],
    [
      3
    ],
    [
      2,
      1
    ],
    [
      2
    ],
    [
      2
    ],
    [
      2
    ]
  ]
}

Expected: [null,null,null,1,-1,null,1,null,-1]

Wizard outline
  1. Step 1: Create deterministic buckets

    Allocate independent collision buckets and map every key to one stable bucket index. All later operations depend on shared storage and a single deterministic indexing rule.

  2. Step 2: Support one mapping per bucket

    Store, retrieve, and miss one mapping before collision chains are introduced. The single-entry case establishes the return contract without hiding collision logic inside the first step.

  3. Step 3: Preserve colliding keys

    Allow multiple distinct keys to coexist in the same bucket and retrieve each by full-key comparison. Modulo identifies a bucket, but only comparing the stored key resolves a collision correctly.

  4. Step 4: Replace exactly one key

    Update an existing key in place without adding a duplicate pair or changing a colliding key. A map owns one current value per key, so put must distinguish replacement from insertion.

  5. Step 5: Remove without collateral loss

    Delete only the matching pair and complete the full hash-map contract. Removal must preserve every other mapping in the collision chain.

Footguns and prerequisites
  • put on an existing key replaces its value rather than adding a duplicate pair.
  • hashing and sets
Reviewed references
Practice prerequisites
  • Resolve a Hash Bucket(opens in a new tab)

    Resolve a Hash Bucket isolates only entries in the computed bucket can match, and an entry matches only when stored_key equals key. That focused state discipline is required when implementing design hashmap as a complete Interview Problem.

Recommended approach and implementation

Use 999 buckets of [key,value] pairs. Scan the selected bucket to replace, read, or remove the exact key.

Why it works: All pairs live in their key's deterministic bucket, and scans compare full keys, so collisions remain distinct. Each operation changes or returns precisely the requested mapping.

class MyHashMap:
    def __init__(self):
        self.size = 999
        self.buckets = [[] for _ in range(self.size)]
    def _bucket_index(self, key):
        return key % self.size
    def put(self, key, value):
        bucket = self.buckets[self._bucket_index(key)]
        for pair in bucket:
            if pair[0] == key:
                pair[1] = value
                return
        bucket.append([key, value])
    def get(self, key):
        for stored_key, value in self.buckets[self._bucket_index(key)]:
            if stored_key == key:
                return value
        return -1
    def remove(self, key):
        bucket = self.buckets[self._bucket_index(key)]
        for index, pair in enumerate(bucket):
            if pair[0] == key:
                bucket.pop(index)
                return
Similar exercises