Skip to content

done design -1#2657

Open
SinAIML wants to merge 1 commit intosuper30admin:masterfrom
SinAIML:master
Open

done design -1#2657
SinAIML wants to merge 1 commit intosuper30admin:masterfrom
SinAIML:master

Conversation

@SinAIML
Copy link
Copy Markdown

@SinAIML SinAIML commented Apr 15, 2026

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Implement Hash Set (design-hashset.py)

Your solution is correct in terms of functionality: it passes all the operations correctly. However, there are important considerations:

  1. Space Efficiency: Your solution allocates an array of size 10^6 + 1 regardless of how many keys are actually stored. This uses about 10^6 * (size of boolean) bytes, which is approximately 1 MB for booleans (each boolean might be 1 byte, but in Python, booleans are objects and might use more memory). This is acceptable for the constraints (keys up to 10^6 and at most 10^4 operations), but it is not efficient in terms of memory. In real-world scenarios, we might need to handle larger key ranges with limited memory.

  2. Design: The problem asks to design a HashSet without using built-in hash table libraries. Your solution uses a direct array index, which is a form of direct addressing. While this is technically not using a built-in hash table, it is not the intended solution. The reference solution uses a hashing technique with buckets to handle collisions and reduce memory usage.

  3. Improvement: Consider implementing a hash-based solution with buckets and linked lists or arrays for each bucket. This would reduce memory usage and be more scalable. For example, you can use a list of buckets (each bucket being a list) and use a hash function to map keys to buckets. Then, within each bucket, you can store the keys. This approach uses memory proportional to the number of keys stored, not the maximum key value.

Here's a simple example of a better approach in Python:

class MyHashSet:
    def __init__(self):
        self.size = 1000
        self.buckets = [[] for _ in range(self.size)]
    
    def _hash(self, key):
        return key % self.size

    def add(self, key):
        bucket_index = self._hash(key)
        bucket = self.buckets[bucket_index]
        if key not in bucket:
            bucket.append(key)

    def remove(self, key):
        bucket_index = self._hash(key)
        bucket = self.buckets[bucket_index]
        if key in bucket:
            bucket.remove(key)

    def contains(self, key):
        bucket_index = self._hash(key)
        bucket = self.buckets[bucket_index]
        return key in bucket

This approach uses chaining to handle collisions. The time complexity for each operation is O(n) in the worst case for a bucket, but with a good hash function and enough buckets, it is efficient on average.

  1. Code Quality: Your code is clean and readable. However, note that in Python, using a list of booleans of fixed size is not the most Pythonic way. Also, the array initialization [False] * (10**6 + 1) creates a list with 10^6+1 elements, which might be memory-intensive.

VERDICT: NEEDS_IMPROVEMENT


Implement Min Stack (design-minstack.py)

Note: The verdict should be based on whether the student's solution is correct and efficient enough to meet the problem requirements.

Now, evaluate the student's solution.

VERDICT: NEEDS_IMPROVEMENT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants