Skip to content

Solution - Implement Queue using only 2 stacks - #2497

Open
MohithN02 wants to merge 4 commits into
super30admin:masterfrom
MohithN02:master
Open

Solution - Implement Queue using only 2 stacks#2497
MohithN02 wants to merge 4 commits into
super30admin:masterfrom
MohithN02:master

Conversation

@MohithN02

Copy link
Copy Markdown

Solution - Implement Queue using only 2 stacks

@super30admin

Copy link
Copy Markdown
Owner

Implement Queue using Stacks (MyHashMap.java)

It appears you have submitted a solution for a different problem (Design HashMap) instead of the problem that was asked (Implement Queue using Stacks).

To correctly solve "Implement Queue using Stacks," you need to:

  1. Create a MyQueue class with two internal stacks (e.g., inSt and outSt).
  2. push(x): Push the element onto the input stack.
  3. peek(): If the output stack is empty, transfer all elements from the input stack to the output stack (this reverses the order, giving FIFO behavior). Then return the top of the output stack.
  4. pop(): Call peek() to ensure the output stack has elements, then pop from the output stack.
  5. empty(): Return true if both stacks are empty.

The key insight is that pushing elements onto one stack and then transferring them to another stack reverses their order, which is exactly what you need to convert LIFO (stack) into FIFO (queue).

Please re-submit your solution for the correct problem.

VERDICT: NEEDS_IMPROVEMENT


Design HashMap (Sample.java)

It looks like you've submitted a solution for "Implement Queue using Stacks" (LeetCode 232) instead of "Design HashMap" (LeetCode 706). These are two completely different problems.

For the Design HashMap problem, you need to:

  1. Create a MyHashMap class with a constructor
  2. Implement put(int key, int value) to insert/update key-value pairs
  3. Implement get(int key) to retrieve the value for a key (return -1 if not found)
  4. Implement remove(int key) to delete a key-value pair

A common approach is to use an array of buckets where each bucket is a linked list (separate chaining) to handle collisions. The hash function typically uses modulo operation on the key.

Please resubmit with the correct solution for the Design HashMap problem.

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