← All problems
Implement Queue using Stacks
Implement a FIFO queue using only two LIFO stacks. The amortized cost of each operation should be O(1). Explain why the amortized bound holds.
Complexity
Time O(1) amortized, Space O(n)
Common pitfalls
- Transferring on every pop (O(n) each)
- Forgetting peek shares pop's logic
Your solution
Log in to save your code and track progress.