Skip to content

Design HashSet - Solution - #2697

Open
MohithN02 wants to merge 1 commit into
super30admin:masterfrom
MohithN02:master
Open

Design HashSet - Solution#2697
MohithN02 wants to merge 1 commit into
super30admin:masterfrom
MohithN02:master

Conversation

@MohithN02

Copy link
Copy Markdown

Solution to Hash Set using Double Hashing Technique and Boolean nested array

@super30admin

Copy link
Copy Markdown
Owner

Design HashSet (Sample.java)

Strengths:

  • Your solution correctly implements the double hashing technique with primary and secondary buckets
  • The lazy initialization of secondary arrays is a smart optimization that saves memory
  • The special handling for bucket == 0 (using bucketItems + 1) is correctly implemented to accommodate the maximum key value of 10^6
  • The code is clean and readable with good variable naming
  • Time complexity of O(1) for all operations is achieved

Areas for Improvement:

  • The hash2 method should be declared as private instead of public for consistency with hash1 and proper encapsulation
  • Consider adding brief comments explaining the approach (e.g., why we use 1000 buckets, the double hashing strategy)
  • The header comments at the top of the file are empty - you should fill in the time/space complexity and any notes about your approach, as this is good practice for documentation

Minor Notes:

  • Your solution is essentially identical to the reference solution, which shows you understood the problem well
  • The approach is optimal for this problem given the constraints

VERDICT: PASS


Min Stack

It appears that you've submitted a solution for a different problem (Design HashSet) instead of the Min Stack problem. The Min Stack problem requires you to implement a stack that supports push, pop, top, and retrieving the minimum element in constant time.

To solve the Min Stack problem correctly, you should:

  1. Use a main stack to store all elements
  2. Use a secondary stack (or a variable) to track the minimum element at each level
  3. When pushing, also push the current minimum onto the min stack
  4. When popping, pop from both stacks
  5. The top of the min stack always gives you the current minimum

Please re-implement the solution for the Min Stack problem. The reference solution uses two stacks - one for the actual values and another to track the minimum at each level.

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.

3 participants