← All problems
Implement Stack using Queues
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.
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.