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
The Challenge: You can only use standard stack operations:
Example:
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 queuepop()- Remove and return the element from the frontpeek()- Return the front element without removing itempty()- 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
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)
โ Linear Growth
Space Complexity
O(n)
Space needed for two stacks to hold all n elements
โก 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)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code