Skip to content

Hashmap and Queue implementations#2437

Open
takchiks wants to merge 3 commits intosuper30admin:masterfrom
takchiks:master
Open

Hashmap and Queue implementations#2437
takchiks wants to merge 3 commits intosuper30admin:masterfrom
takchiks:master

Conversation

@takchiks
Copy link

@takchiks takchiks commented Feb 4, 2026

No description provided.

@super30admin
Copy link
Owner

Your solution for MyQueue is correct and efficient. The use of two stacks follows the standard approach, and the time complexity is amortized O(1) for pop and peek, which is optimal. The code is clean and easy to understand.

However, note that the problem you are solving is "Create Queue using Stacks", but you also included a solution for "MyHashMap" in the same file. This might be a mistake, as the problem does not require implementing a hash map. When submitting solutions, ensure that you only include the relevant code for the problem at hand.

For the MyQueue class:

  • You have correctly implemented all the methods.
  • The logic for transferring elements from pushStack to popStack only when necessary is efficient.
  • The empty method correctly checks both stacks.

One minor point: since the problem states that all calls to pop and peek are valid, you don't strictly need to return -1 in pop and peek when the queue is empty. However, it's good defensive programming to handle such cases. Alternatively, you could throw an exception (like NoSuchElementException) to indicate that the queue is empty, but the problem does not specify the behavior for invalid operations. Given the constraints, your approach is acceptable.

Overall, your solution is excellent. Just be careful to include only the required code for the problem.

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