Design HashSet
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 MyHashSet.add(key), remove(key), and contains(key) without using Python set or dict as the key store.
Starter code
class MyHashSet:
def __init__(self):
passTest cases
basic
{
"operations": [
"MyHashSet",
"add",
"add",
"contains",
"contains",
"add",
"contains",
"remove",
"contains"
],
"arguments": [
[],
[
1
],
[
2
],
[
1
],
[
3
],
[
2
],
[
2
],
[
2
],
[
2
]
]
}Expected: [null,null,null,true,false,null,true,null,false]
Wizard outline
- Step 1: Initialize MyHashSet
Replace the empty starter with the first real state owned by MyHashSet. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.
- Step 2: Assemble the primary transition
Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.
- Step 3: Pass the Wizard Stateful Hashset Basic case
Complete the readable core algorithm for one representative Interview case. The single-key case establishes bucket addressing before collision chains are allowed.
- Step 4: Harden the Wizard Stateful Hashset Collision boundary
Repair the reviewed boundary and pass the complete submission contract. Bucket equality is not key equality; full-key membership preserves unrelated collisions.
Footguns and prerequisites
- Two different keys can share a bucket; bucket occupancy alone does not prove membership.
- 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 hashset as a complete Interview Problem.
Recommended approach and implementation
Allocate 999 list buckets. Hash with key%999, append only when absent, and remove only the matching key.
Why it works: Every key is stored only in its deterministic bucket. Scanning that bucket distinguishes collisions, so add preserves uniqueness, remove affects only its key, and contains exactly reflects stored membership.
class MyHashSet:
"""
Checkpoint 1: initialize the state owned by this Interview contract.
Checkpoint 2: assemble the primary transition without hiding the boundary.
"""
def __init__(self):
self.size = 999
self.buckets = [[] for _ in range(self.size)]
def add(self, key):
bucket = self.buckets[key % self.size]
if key not in bucket: bucket.append(key)
def remove(self, key):
bucket = self.buckets[key % self.size]
if key in bucket: bucket.remove(key)
def contains(self, key):
return key in self.buckets[key % self.size]