← All problems

Implement Queue using Stacks

Easy stacksqueuesdesign LeetCode ↗
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.

Hints (0/3 revealed)

Log in to reveal hints progressively.

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.