Loading practice problems…
Loading your practice workspace…
Loading code editor…
27 / 29
Sign in
Algorithms/Queue

Implement Queue Using Stacks

easy30 min

Process queue operations using only stack-style push and pop behavior internally.

Return one output per operation. Constructor and push produce null; pop and peek return values; empty returns a boolean.

Example 1

Inputoperations = ["MyQueue", "push", "push", "peek", "pop", "empty"], values = [[], [1], [2], [], [], []]

Output[null, null, null, 1, 1, false]

Example 2

Inputoperations = ["MyQueue", "push", "pop", "empty"], values = [[], [7], [], []]

Output[null, null, 7, true]

Constraints

  • At most 100 operations are performed.
  • pop and peek are called only on a non-empty queue.
QueueStackDesign