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]

1 entered before 2, so peek shows 1 and pop removes it. The queue still holds 2, so empty returns false. The constructor and pushes return null.

Example 2

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

Output[null, null, 7, true]

After pushing 7, pop removes it and returns 7. The queue then has no items, so empty returns true.

Constraints

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