Design HashSet - Problem
Design a HashSet without using built-in hash table libraries.
Your mission is to implement a
Implement the following methods:
•
•
•
This problem tests your understanding of hash functions, collision handling, and fundamental data structure design principles.
Your mission is to implement a
MyHashSet class from scratch that provides the core functionality of a hash set data structure. A HashSet is a collection that stores unique elements and provides fast insertion, deletion, and lookup operations.Implement the following methods:
•
add(key) - Inserts the value key into the HashSet•
contains(key) - Returns true if the value key exists in the HashSet, false otherwise•
remove(key) - Removes the value key from the HashSet (no-op if key doesn't exist)This problem tests your understanding of hash functions, collision handling, and fundamental data structure design principles.
Input & Output
example_1.py — Basic Operations
$
Input:
["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"]
[[], [1], [2], [1], [3], [2], [2], [2], [2]]
›
Output:
[null, null, null, true, false, null, true, null, false]
💡 Note:
MyHashSet is initialized, add(1), add(2), contains(1) returns true, contains(3) returns false, add(2) does nothing since 2 already exists, contains(2) returns true, remove(2), contains(2) returns false
example_2.py — Duplicate Handling
$
Input:
["MyHashSet", "add", "add", "add", "contains"]
[[], [5], [5], [5], [5]]
›
Output:
[null, null, null, null, true]
💡 Note:
Adding the same element multiple times should only store it once. The set should still contain the element after multiple add operations.
example_3.py — Remove Non-existent
$
Input:
["MyHashSet", "remove", "add", "remove", "remove", "contains"]
[[], [1], [1], [1], [1], [1]]
›
Output:
[null, null, null, null, null, false]
💡 Note:
Removing a non-existent element should do nothing. After removing an element that was added, contains should return false.
Visualization
Tap to expand
Understanding the Visualization
1
Design Hash Function
Create a function that maps book IDs to specific sections (buckets) evenly
2
Handle Collisions
When multiple books map to the same section, store them as a list within that section
3
Optimize Access
Each operation now only searches within the relevant section, not the entire library
Key Takeaway
🎯 Key Insight: Hash functions transform O(n) searches into O(1) operations by intelligently distributing data across multiple buckets, like organizing a library into sections for instant book location.
Time & Space Complexity
Time Complexity
O(1)
Average case O(1) due to hash function distributing elements evenly across buckets. Worst case O(n) if all elements hash to same bucket
✓ Linear Growth
Space Complexity
O(n + k)
O(n) for storing n elements plus O(k) for k buckets array
⚡ Linearithmic Space
Constraints
- 0 ≤ key ≤ 106
- At most 104 calls will be made to add, remove, and contains
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code