Skip to content

Complete Design-2#2456

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

Complete Design-2#2456
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

Create Queue using Stacks (Problem5.java)

It appears there has been a mix-up in the problem you were solving. The problem given was to implement a queue using two stacks, but your solution is for a hashmap. This indicates that you might have solved a different problem or there was a misunderstanding.

For the correct problem (Create Queue using Stacks), you need to use two stacks to simulate a queue. The typical approach is to have one stack for input and another for output. When you push an element, you simply push it to the input stack. When you need to pop or peek, if the output stack is empty, you transfer all elements from the input stack to the output stack (which reverses the order). Then you can pop or peek from the output stack. This ensures that the first element pushed ends up at the top of the output stack.

Here is a correct implementation in Java for reference:

class MyQueue {
    private Stack<Integer> input;
    private Stack<Integer> output;

    public MyQueue() {
        input = new Stack<>();
        output = new Stack<>();
    }
    
    public void push(int x) {
        input.push(x);
    }
    
    public int pop() {
        peek();
        return output.pop();
    }
    
    public int peek() {
        if (output.empty()) {
            while (!input.empty()) {
                output.push(input.pop());
            }
        }
        return output.peek();
    }
    
    public boolean empty() {
        return input.empty() && output.empty();
    }
}

Please review the problem statement again and ensure you are solving the correct problem. Also, note that the constraints for this problem are different (calls up to 100) compared to a hashmap problem. If you have any questions about the queue using stacks implementation, feel free to ask.

VERDICT: NEEDS_IMPROVEMENT


Implement Hash Map (Sample.java)

It seems there has been a misunderstanding. The problem was to implement a hash map, but you have implemented a queue using two stacks. This is a completely different data structure. For the hash map problem, you need to design a class that stores key-value pairs and supports operations like put, get, and remove.

To implement a hash map, you should consider using an array of linked lists (or another data structure) to handle collisions. Each bucket in the array can store multiple key-value pairs. You'll need to define a hash function to map keys to buckets.

Here are some steps to get started:

  1. Define a class for nodes in the linked list (each node should have key, value, and next pointer).
  2. Initialize an array of nodes (buckets) in the constructor.
  3. Implement a hash function (e.g., key % number_of_buckets).
  4. For the put method, find the bucket index, then traverse the linked list to check if the key exists (update if found, else append).
  5. For the get method, find the bucket index and then search the linked list for the key.
  6. For the remove method, find the bucket index and then remove the node with the given key.

Please review the problem statement again and try to implement the correct solution. If you have any questions about hash maps or collision handling, feel free to ask.

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