Skip to content

Completed Design-1 Solved Problem1 - Design hashset and Problem 2 - Min Stack#2661

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

Completed Design-1 Solved Problem1 - Design hashset and Problem 2 - Min Stack#2661
anirudhv98 wants to merge 1 commit intosuper30admin:masterfrom
anirudhv98:master

Conversation

@anirudhv98
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Implement Hash Set (Problem1.cs)

Your solution is well-implemented and follows the double hashing approach correctly. Here are some strengths and areas for improvement:

Strengths:

  • You have correctly identified the need for double hashing and implemented two hash functions appropriately.
  • You handled the edge case for the first bucket (index 0) by allocating an array of size 1001 to accommodate the key 1000000.
  • The code is clean, readable, and well-commented.

Areas for Improvement:

  • In the Contains method, you compute the second hash (hash2) even when the primary bucket (buckets[hash1]) is null. This is unnecessary and can be optimized by first checking if the bucket is null and then computing the second hash only if the bucket exists. For example:
    public bool Contains(int key)
    {
        int hash1 = HashFunction1(key);
        if (buckets[hash1] == null)
            return false;
        int hash2 = HashFunction2(key);
        return buckets[hash1][hash2];
    }
    
  • Although not critical, you could consider making the constants (1000, 1000) as private readonly fields for better maintainability.

Overall, your solution is correct and efficient. With a minor optimization in the Contains method, it would be even better.

VERDICT: PASS


Implement Min Stack (Problem2.cs)

Your solution is well-thought-out and correctly implements the required functionality with constant time operations. Here are some strengths and areas for improvement:

Strengths:

  • You correctly identified the need to store the minimum value along with each element to achieve O(1) time for getMin.
  • The code is clean and well-commented, which makes it easy to understand.
  • You handle the case when the stack becomes empty in the Pop method by resetting minimumSoFar to Int32.MaxValue.

Areas for Improvement:

  • In the Pop method, you call minStack.Pop() and then immediately use the popped value to check if it was the minimum. However, note that after popping, the stack has changed. Your approach is correct because you are only using the popped value for comparison, but it might be clearer to store the popped array in a variable first. For example:
    int[] popped = minStack.Pop();
    if (popped[0] == minimumSoFar) {
        // then update minimumSoFar
    }
    
    This avoids any confusion about the state of the stack.
  • Alternatively, you can avoid the call to GetMin() in the Pop method by directly accessing the new top's stored minimum if the stack is not empty. For instance:
    int[] popped = minStack.Pop();
    if (popped[0] == minimumSoFar) {
        if (minStack.Count > 0) {
            minimumSoFar = minStack.Peek()[1];
        } else {
            minimumSoFar = Int32.MaxValue;
        }
    }
    
    This is slightly more efficient because GetMin() calls minStack.Peek()[1], which is the same as what you are doing, but it avoids an extra method call. However, the current code is acceptable.
  • Consider initializing minimumSoFar to Int32.MaxValue in the constructor explicitly (which you already do) to ensure it starts correctly.

Overall, your solution is correct and efficient. The minor improvements suggested are mainly for clarity and style.

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