Implement Queue using Stacks Problem
Implement Queue using Stacks Problem — ExecCode Easy DSA Practice
Solve the Implement Queue using Stacks problem on ExecCode. Free online easy DSA practice in Design. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement the MyQueue class: - MyQueue() Initializes the object. - 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 without removing it. - boolean empty() Returns true if the queue is empty, false otherwise.
Examples
Input {"ops": ["push", "push", "push", "peek", "push", "pop", "pop", "peek", "empty"], "vals": [1, 2, 3, 0, 4, 0, 0, 0, 0]}; Output [1, 1, 2, 3, False]. Input {"ops": ["push", "push", "peek", "pop", "empty"], "vals": [1, 2, 0, 0, 0]}; Output [1, 1, False]. Input {"ops": ["push", "pop", "empty"], "vals": [5, 0, 0]}; Output [5, True]
Constraints
1 ≤ x ≤ 9 At most 100 calls will be made to push, pop, peek, and empty. All the calls to pop and peek are valid.
Practice Implement Queue using Stacks free on ExecCode. Browse DSA problems, topic map, and placement guides.