Skip to content

Design-1 Implemented min-stack#2650

Open
PavanKumarBollam wants to merge 3 commits intosuper30admin:masterfrom
PavanKumarBollam:master
Open

Design-1 Implemented min-stack#2650
PavanKumarBollam wants to merge 3 commits intosuper30admin:masterfrom
PavanKumarBollam:master

Conversation

@PavanKumarBollam
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Implement Hash Set (Sample.java)

It appears there has been a misunderstanding. The problem was to implement a HashSet, but your solution is for a MinStack. Please review the problem statement carefully. For the HashSet problem, you need to design a data structure that supports add, remove, and contains operations without using built-in hash table libraries.

To implement MyHashSet, you might consider using techniques like chaining with linked lists or using a boolean array with double hashing (as shown in the reference solution). The reference solution uses a 2D boolean array with two levels of hashing to handle collisions.

Here are some steps to get you started on the correct problem:

  1. Define a class named MyHashSet.
  2. Consider the constraints: keys are in the range [0, 10^6] and there are up to 10^4 operations.
  3. You need to decide on a hashing strategy. The reference solution uses 1000 primary buckets and 1000 secondary buckets, with a special case for the first bucket to handle the key 10^6.
  4. Implement the add, remove, and contains methods using your chosen hashing.

VERDICT: NEEDS_IMPROVEMENT


Implement Min Stack

Your solution is well-structured and efficient. Here are some points to consider:

  1. Correctness: Your solution correctly handles the operations. However, you should note that the problem constraints ensure that pop, top, and getMin are called only on non-empty stacks, so you don't need to handle empty stack cases in those methods. But it's always good to be cautious in real-world scenarios.

  2. Code Quality: The code is clean and readable. However, adding comments to explain your approach, especially why you push to minStack when val <= minStack.peek(), would be helpful for others reading your code.

  3. Edge Cases: You have considered duplicates by using <= when pushing to minStack. This is correct. However, one common alternative is to store pairs in the minStack (value and count) to save space when there are many duplicates. But your current approach is simpler and acceptable.

  4. Initialization: Your minStack is initialized as empty. The reference solution initializes the minStack with a large value to avoid null checks. Since the problem guarantees that getMin is called only when the stack is non-empty, your approach is safe. But if you were to extend this for general use, you might want to add a check in getMin to throw an exception if the stack is empty.

  5. Efficiency: Your solution is efficient with O(1) time for all operations and O(n) space. Well done.

Overall, your solution is correct 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.

3 participants