Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Implement Queue using Stacks LeetCode Solution – Queslers

Problem – Implement Queue using Stacks

Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (pushpeekpop, and empty).

Implement the MyQueue class:

  • void push(int x) Pushes element x to the back of the queue.
  • int pop() Removes the element from the front of the queue and returns it.
  • int peek() Returns the element at the front of the queue.
  • boolean empty() Returns true if the queue is empty, false otherwise.


  • You must use only standard operations of a stack, which means only push to toppeek/pop from topsize, and is empty operations are valid.
  • Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack’s standard operations.

Example 1:

["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
[null, null, null, 1, 1, false]

MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false


  • 1 <= x <= 9
  • At most 100 calls will be made to pushpoppeek, and empty.
  • All the calls to pop and peek are valid.

Follow-up: Can you implement the queue such that each operation is amortizedO(1) time complexity? In other words, performing n operations will take overall O(n) time even if one of those operations may take longer.

Implement Queue using Stacks LeetCode Solution in Java

class MyQueue {

    Stack<Integer> input = new Stack();
    Stack<Integer> output = new Stack();
    public void push(int x) {

    public void pop() {

    public int peek() {
        if (output.empty())
            while (!input.empty())
        return output.peek();

    public boolean empty() {
        return input.empty() && output.empty();

Implement Queue using Stacks LeetCode Solution in C++

class Queue {
    stack<int> input, output;

    void push(int x) {

    void pop(void) {

    int peek(void) {
        if (output.empty())
            while (input.size())
                output.push(, input.pop();

    bool empty(void) {
        return input.empty() && output.empty();

Implement Queue using Stacks LeetCode Solution in Python

class Queue(object):
    def __init__(self):
        initialize your data structure here.
        self.inStack, self.outStack = [], []

    def push(self, x):
        :type x: int
        :rtype: nothing

    def pop(self):
        :rtype: nothing

    def peek(self):
        :rtype: int
        return self.outStack[-1]

    def empty(self):
        :rtype: bool
        return (not self.inStack) and (not self.outStack) 
    def move(self):
        :rtype nothing
        if not self.outStack:
            while self.inStack:
Implement Queue using Stacks LeetCode Solution Review:

In our experience, we suggest you solve this Implement Queue using Stacks LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Implement Queue using Stacks LeetCode Solution

Find on LeetCode


I hope this Implement Queue using Stacks LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *