Skip to content
Onsite

Queue and Stack Queries

EasyP3Coding

There is a queue (first in, first out) and a stack (last in, first out), both empty at the start. Process queries in order. Each query is one of:

  • [1, x]: add x to the back of the queue.
  • [2, x]: push x onto the stack.
  • [3]: report the element at the front of the queue, or -1 if the queue is empty.
  • [4]: report the element on top of the stack, or -1 if the stack is empty.
  • [5]: remove the front element of the queue and push it onto the stack. If the queue is empty, report -1 and change nothing; otherwise report nothing.

Return everything reported, in order.

Example 1

Input
queries = [[1,5],[1,6],[3],[5],[4],[3]]
Output
[5,5,6]
Why
Front is 5. Moving 5 to the stack makes the top 5. The new front is 6.

Constraints

  • 1 <= queries.length <= 10^5
  • 1 <= x <= 10^9

Hints

Run checks the 3 visible tests, Submit all 9.