Skip to content

chore: Completed Design-1#2647

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

chore: Completed Design-1#2647
pkmaster21 wants to merge 1 commit intosuper30admin:masterfrom
pkmaster21:master

Conversation

@pkmaster21
Copy link
Copy Markdown

No description provided.

@pkmaster21
Copy link
Copy Markdown
Author

pkmaster21 commented Apr 7, 2026

i realized i implemented the hashset using a built in python hash which wasnt what the question was asking, will update with new solution in a bit

done

@super30admin
Copy link
Copy Markdown
Owner

Implement Hash Set (design_hashset.py)

Strengths:

  • The code is clean and well-commented.
  • The student has thought about time and space complexity.

Areas for improvement:

  1. The implementation uses chaining with lists, which leads to O(n) worst-case time for operations. Consider using a more efficient approach like double hashing (as in the reference solution) or at least increase the number of buckets to reduce the chance of collisions.
  2. The current number of buckets (1000) is fixed. For better performance, you might want to use a prime number of buckets to reduce collisions.
  3. The contains method uses in which is O(n) in the bucket size. This is inefficient. In the reference solution, the contains is O(1) because it uses direct indexing.
  4. The reference solution uses a 2D boolean array to mark the presence of keys. This is more space-efficient because it uses booleans (1 byte) instead of integers (24 bytes in Python). Also, it avoids storing the keys themselves.
  5. The student's solution does not handle the entire range of keys (0 to 10^6) correctly? Actually, it does because the bucket index is computed modulo 1000, and the list can store any key. But the reference solution is designed to handle exactly the range [0,10^6] without storing the keys.

Suggestion: The problem constraints say that keys are in [0,10^6]. You can use a boolean array of size 10^6+1. This would be simpler and more efficient. For example:

class MyHashSet:
    def __init__(self):
        self.size = 1000001
        self.arr = [False] * self.size

    def add(self, key):
        self.arr[key] = True

    def remove(self, key):
        self.arr[key] = False

    def contains(self, key):
        return self.arr[key]

This is O(1) time and O(1) space (with constant 10^6+1). This is acceptable because the constraints are fixed.

Alternatively, if you want to use a hash table with chaining, you should use a larger number of buckets (e.g., 10000) to reduce the average chain length.

VERDICT: NEEDS_IMPROVEMENT


Implement Min Stack (design_minstack.py)

Your solution is correct and efficient. You have correctly understood the concept of using two stacks to maintain the minimum value in constant time. The code is clean and well-commented.

One minor point: in the push method, you check if len(self.minStack) == 0 or val <= self.minStack[-1]. This is correct, but note that when the minStack is empty, you are pushing the value. However, in your init, you initialize both stacks as empty. So when the first element is pushed, the minStack will be empty, and it will be added. This is correct.

Another point to consider: what if we push a value that is greater than the current minimum? Then it is not added to minStack. This is correct because the minimum doesn't change. But when we pop a value that is not the current minimum, we don't need to change the minStack. So your pop method is correct.

Overall, this is a solid implementation. However, there is an alternative approach that uses a single stack and stores tuples (value, current_min) which also works in O(1) time and O(n) space. But your two-stack approach is standard and efficient.

Keep up the good work!

VERDICT: PASS

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