Implement Queue using Stacks

Easy

Description

Implement a first-in-first-out (FIFO) queue using only two stacks. The implemented queue should support push, pop, peek, and empty operations.

Examples

Input:["MyQueue", "push", "push", "peek", "pop", "empty"] with [[], [1], [2], [], [], []]
Output:[null, null, null, 1, 1, false]
Explanation:

Push 1 and 2, peek returns 1, pop returns 1, not empty.

Input:["MyQueue", "push", "push", "push", "pop", "peek", "pop", "pop", "empty"]
Output:[null, null, null, null, 3, 4, 4, 5, true]
Explanation:

Initialize queue, push 3, 4, and 5. Pop returns 3 (first in), peek shows 4 is next, pop returns 4, pop returns 5, queue is now empty.

Input:["MyQueue", "push", "pop", "push", "push", "peek", "push", "pop", "pop", "empty"]
Output:[null, null, 7, null, null, 8, null, 8, 9, false]
Explanation:

Initialize queue, push 7 then immediately pop it (returns 7). Push 8 and 9, peek shows 8. Push 6, then pop twice getting 8 and 9. Queue still contains 6 so not empty.

Constraints

  • 1 ≤ x ≤ 9
  • At most 100 calls will be made to push, pop, peek, and empty.

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!