Implement Queue using Stacks - Problem

Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue.

Implement the MyQueue class:

  • void push(int x) Pushes element x to the back of the queue
  • int pop() Removes the element from the front of the queue and returns it
  • int peek() Returns the element at the front of the queue
  • boolean empty() Returns true if the queue is empty, false otherwise

Notes:

  • You must use only standard operations of a stack (push to top, peek/pop from top, size, and is empty)
  • Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque as long as you use only a stack's standard operations

Input & Output

Example 1 — Basic Queue Operations
$ Input: operations = ["MyQueue", "push", "push", "peek", "pop", "empty"], values = [null, 1, 2, null, null, null]
Output: [null, null, null, 1, 1, false]
💡 Note: Create queue, push 1 and 2, peek returns 1 (front), pop returns 1, queue not empty (still has 2)
Example 2 — Multiple Operations
$ Input: operations = ["MyQueue", "push", "push", "push", "pop", "pop", "empty"], values = [null, 1, 2, 3, null, null, null]
Output: [null, null, null, null, 1, 2, false]
💡 Note: Push 1,2,3 then pop twice: first pop gets 1, second pop gets 2, queue still has 3 so not empty
Example 3 — Empty Queue Check
$ Input: operations = ["MyQueue", "push", "pop", "empty"], values = [null, 5, null, null]
Output: [null, null, 5, true]
💡 Note: Push 5, pop 5, now queue is empty so empty() returns true

Constraints

  • 1 ≤ x ≤ 9
  • At most 100 calls will be made to push, pop, peek, and empty
  • All the calls to pop and peek are valid

Visualization

Tap to expand
Implement Queue using Stacks Lazy Transfer Approach INPUT Operations: ["MyQueue", "push", "push", "peek", "pop", "empty"] values: [null,1,2,null,null,null] Two Stacks Structure: Stack1 (input) 2 1 Stack2 (output) empty FIFO using LIFO stacks ALGORITHM STEPS 1 push(1), push(2) Add to Stack1 (input) 2 1 2 peek() - Lazy Transfer Move all to Stack2 on demand S1 1 2 S2 3 peek()/pop() Return/remove Stack2 top 1 2 1 4 empty() Check if both stacks empty Stack2 has element: false FINAL RESULT Operation Trace: Operation Return MyQueue() null push(1) null push(2) null peek() 1 pop() 1 empty() false Output Array: [null, null, null, 1, 1, false] Final Queue State: Queue (front to back) 2 front FIFO OK Key Insight: Lazy Transfer Optimization Instead of transferring elements on every operation, we only transfer from Stack1 to Stack2 when Stack2 is empty and we need to peek/pop. This gives amortized O(1) time complexity for all operations. Push: O(1) always | Pop/Peek: O(1) amortized | Space: O(n) for both stacks combined. TutorialsPoint - Implement Queue using Stacks | Lazy Transfer Approach
Asked in
Amazon 45 Microsoft 32 Apple 28 Google 25
185.0K Views
High Frequency
~25 min Avg. Time
2.8K 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