Skip to content

Design 2#2445

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

Design 2#2445
pratul2789 wants to merge 1 commit intosuper30admin:masterfrom
pratul2789:master

Conversation

@pratul2789
Copy link

Implemeted Queue using Stacks.
Designed a HashMap

@super30admin
Copy link
Owner

Your implementation of MyQueue using two stacks is correct and efficient. The code is clean and well-organized. The use of a private rebalance function to transfer elements from st1 to st2 when needed is a good practice.

Strengths:

  • Correct implementation of all required methods.
  • Amortized O(1) time complexity for operations.
  • Clear and readable code.

Areas for improvement:

  • Although your code is correct, you can avoid duplicating the rebalance call in both pop and peek by having pop call peek to get the top element and then remove it. However, this is a minor point and your current approach is acceptable.
  • Note that the problem statement is for a general queue using stacks, and your solution is in C++. Make sure that you are submitting only the required code for the problem. The HashMap.cpp file seems to be for a different problem and should not be included.

One potential issue: in your rebalance function, you transfer all elements from st1 to st2. This is correct. However, ensure that you only do this when st2 is empty. Your condition in pop and peek checks for st2.empty(), which is correct.

Keep up the good work!

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