Skip to content

complete Desgin-1#2648

Open
dhruvil15 wants to merge 2 commits intosuper30admin:masterfrom
dhruvil15:master
Open

complete Desgin-1#2648
dhruvil15 wants to merge 2 commits intosuper30admin:masterfrom
dhruvil15:master

Conversation

@dhruvil15
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Implement Hash Set (.idea/.gitignore)

It seems there was a misunderstanding in the submission. The provided file is a .gitignore file, which is used for version control to exclude certain files from being tracked. This is not a solution to the coding problem.

To solve the problem, you need to implement a HashSet class with the methods add, remove, and contains. You should use a data structure that allows efficient insertion, deletion, and lookup. The reference solution uses a 2D boolean array with double hashing to handle collisions.

Here are some steps to get started:

  1. Define a class named MyHashSet.
  2. Initialize necessary variables, such as the number of buckets and a storage array.
  3. Implement the add method to insert a key into the set.
  4. Implement the remove method to delete a key if it exists.
  5. Implement the contains method to check if a key is present.

You should also consider edge cases, such as handling the maximum key value (10^6) and ensuring the solution is efficient within the constraints.

VERDICT: NEEDS_IMPROVEMENT


Implement Min Stack (.idea/Design-1.iml)

It seems you have submitted the wrong file. The file you provided is an IntelliJ IDEA module configuration file (Design-1.iml), which is not the solution code. Please check your submission and resubmit your actual Java source code file (likely named MinStack.java) that contains your implementation of the MinStack class.

To solve this problem, you need to design a stack that supports push, pop, top, and getMin in constant time. You can use two stacks: one to store the elements and another to store the minimum values. Alternatively, you can use a single stack with each element being a pair (value, current_min).

Here is an example of how you might implement it using two stacks:

class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }
    
    public void push(int val) {
        stack.push(val);
        if (minStack.isEmpty() || val <= minStack.peek()) {
            minStack.push(val);
        } else {
            minStack.push(minStack.peek());
        }
    }
    
    public void pop() {
        stack.pop();
        minStack.pop();
    }
    
    public int top() {
        return stack.peek();
    }
    
    public int getMin() {
        return minStack.peek();
    }
}

Note: This implementation pushes the current minimum to the minStack on every push, so the minStack always has the same size as the main stack. This ensures that when we pop, we can always pop from the minStack and the top of the minStack will be the current minimum.

Another efficient approach is to only push to the minStack when a new minimum is found, but then you have to handle the pop operation differently. However, the above method is straightforward and meets the O(1) time requirement for each operation.

VERDICT: NEEDS_IMPROVEMENT

@dhruvil15
Copy link
Copy Markdown
Author

@super30admin I have the sample.java file which has the implementation

@dhruvil15
Copy link
Copy Markdown
Author

@super30admin I have also added the solution for MinStack, which was Problem 3. The Github repo was re-directing to this Design-1 repo for pushing Problem solution.

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