Implement Queue using Stacks - Problem
Implement a Queue using Two Stacks

Your mission is to create a First In, First Out (FIFO) queue data structure using only two stacks. Think of a queue like a line at a coffee shop - the first person in line gets served first!

You need to implement the MyQueue class with these operations:
  • push(x) - Add element x to the back of the queue
  • pop() - Remove and return the element from the front
  • peek() - Return the front element without removing it
  • empty() - Check if the queue is empty

The Challenge: You can only use standard stack operations: push, pop, top, and empty. No direct access to queue operations allowed!

Example:
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false

Input & Output

example_1.py โ€” Basic Operations
$ Input: ["MyQueue", "push", "push", "peek", "pop", "empty"] [[], [1], [2], [], [], []]
โ€บ Output: [null, null, null, 1, 1, false]
๐Ÿ’ก Note: Create queue, push 1 and 2. peek() returns 1 (front element). pop() removes and returns 1. empty() returns false since 2 is still in queue.
example_2.py โ€” Single Element
$ Input: ["MyQueue", "push", "pop", "empty"] [[], [42], [], []]
โ€บ Output: [null, null, 42, true]
๐Ÿ’ก Note: Push single element 42, pop it (returns 42), then queue becomes empty.
example_3.py โ€” Multiple Operations
$ Input: ["MyQueue", "push", "push", "push", "pop", "push", "peek", "pop", "pop"] [[], [1], [2], [3], [], [4], [], [], []]
โ€บ Output: [null, null, null, null, 1, null, 2, 2, 3]
๐Ÿ’ก Note: Push 1,2,3 then pop 1. Push 4. Queue now has [2,3,4]. peek() returns 2, pop() returns 2 and 3.

Visualization

Tap to expand
Two-Stack Queue ImplementationInput StackPush hereOutput StackPop/Peek here321123Transfer when output empty(Reverses order!)NEWโ† push(NEW)1pop() โ†’โšก Amortized O(1) Operationsโ€ข Push: Always O(1) to input stackโ€ข Pop/Peek: O(1) amortizedโ€ข Each element moved โ‰ค 2 times totalโ€ข Perfect FIFO behavior guaranteed!
Understanding the Visualization
1
Push Phase
All new elements go into the input stack, just like stacking plates
2
Transfer Trigger
When we need to pop/peek and output stack is empty, transfer all elements
3
Order Reversal
The transfer process reverses the order, making FIFO access possible
4
Efficient Access
Now we can pop/peek from output stack in proper queue order
Key Takeaway
๐ŸŽฏ Key Insight: Using two stacks cleverly transforms LIFO operations into FIFO behavior through strategic element transfers, achieving optimal amortized performance!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1) amortized

Each element is transferred at most twice across its lifetime. Push is always O(1), pop/peek are amortized O(1)

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space needed for two stacks to hold all n elements

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค x โ‰ค 109
  • At most 100 calls will be made to push, pop, peek, and empty
  • All calls to pop and peek are valid (queue is not empty)
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28 Apple 22
67.0K Views
High Frequency
~15 min Avg. Time
2.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen