Skip to content

Design 1#2646

Open
MeghaN28 wants to merge 5 commits intosuper30admin:masterfrom
MeghaN28:master
Open

Design 1#2646
MeghaN28 wants to merge 5 commits intosuper30admin:masterfrom
MeghaN28:master

Conversation

@MeghaN28
Copy link
Copy Markdown

@MeghaN28 MeghaN28 commented Apr 7, 2026

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Implement Hash Set (Sample.java)

Your solution is generally correct and follows the separate chaining approach. Well done on preventing duplicate keys in the add method. However, there are a few areas for improvement:

  1. The modulus calculation in the contains method is more complex than necessary. Since the key is always non-negative, you can simply use key % size without the extra + size) % size. This will make the code clearer and slightly more efficient.

  2. In the remove method, instead of iterating and then removing the found entry, you could use an iterator to remove the element during iteration. This would be more efficient and avoid the need for a temporary variable. For example:

    Iterator<Entry> it = set[bucket].iterator();
    while (it.hasNext()) {
         Entry e = it.next();
         if (e.key == key) {
             it.remove();
             return;
         }
    }
    

    This way, you break early and remove without having to traverse the entire list.

  3. Your code does not implement rehashing, which is mentioned in your comments. While it's not strictly required for the problem (given the constraints), it's important to note that without rehashing, the performance might degrade if the number of elements becomes large relative to the bucket size. However, with a fixed bucket size of 769 and only up to 10^4 operations, it should be acceptable.

  4. The class name should be consistent with the problem. The problem expects a class named MyHashSet, which you have provided. But the outer class in the sample is named Main – this is fine for testing, but in a real submission, only MyHashSet should be submitted.

  5. Consider adding a null check in the contains method for the bucket to avoid null pointer exceptions. You already have this in the remove method, but in contains, you check if (set[bucket] != null) which is correct. Actually, in your contains method, you do have the null check – good job.

Overall, your solution is functional and passes the basic test cases. With minor improvements, it can be more efficient and cleaner.

VERDICT: PASS


Implement Min Stack

It seems there might be a misunderstanding. The problem you were asked to solve is to design a MinStack, but your solution is for a MyHashSet (a hash set). This indicates that you might have confused the problem with another one.

For the MinStack problem, you need to design a stack that can push, pop, and return the top element in constant time, as well as return the minimum element in constant time. The common approach is to use two stacks: one to store all the elements and another to store the minimum values. Alternatively, you can use a single stack that stores pairs (value, current_min).

I recommend revisiting the problem statement and understanding the requirements. The example input and output should help clarify what is expected.

Your current solution for MyHashSet is well-structured for that problem, but it does not apply here. For the MinStack, you should start from scratch.

Strengths:

  • Your code for MyHashSet is clear and uses separate chaining correctly.
  • You have handled collisions and rehashing (though rehashing is not implemented in this code, but the comment mentions it).

Areas for improvement:

  • Ensure you are solving the correct problem. Double-check the problem title and description.
  • For MinStack, you need to implement a stack, not a hash set.
  • The time complexity for MinStack operations must be O(1) per operation, which your current solution does not address.

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