← All problems

Implement Stack using Queues

Easy stacksqueuesdesign LeetCode ↗
Implement a LIFO stack using only standard FIFO queue operations (push to back, peek/pop from front, size, isEmpty). Support push, pop, top, and empty. You may use two queues; make pop/top efficient and explain the trade-off versus the alternative layout.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Push O(n), pop/top/empty O(1); space O(n).

Common pitfalls

  • Rotating size times instead of size-1, putting x at the back again.
  • Forgetting that queue pop removes from the front, not the back.
  • Not handling pop/top on an empty stack.

Your solution

Log in to save your code and track progress.