Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

85. Implement Queue using Stacks

EasyDay 25 of 60Stack & Queue

Build a first in, first out queue MyQueue using only two stacks. Support push(x) (add to the back), pop() (remove and return the front), peek() (return the front) and empty(). You may only use standard stack operations: push to top, pop or peek from top, size and is empty. pop and peek are only called on a non-empty queue.

Example 1
Input: ops = ["MyQueue","push","push","peek","pop","empty"], args = [[],[1],[2],[],[],[]]
Output: [null,null,null,1,1,false]
Constraints
  • 1 <= x <= 9
  • At most 100 calls in total
  • pop and peek are always valid
Hints
Frequently asked at: Microsoft, Amazon, Apple, Zoho
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for ₹200/ 30 min