Implement Stack using Queues - Problem

Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).

Implement the MyStack class:

  • void push(int x) - Pushes element x to the top of the stack
  • int pop() - Removes the element on the top of the stack and returns it
  • int top() - Returns the element on the top of the stack
  • boolean empty() - Returns true if the stack is empty, false otherwise

Notes:

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

Input & Output

Example 1 — Basic Stack Operations
$ Input: operations = ["MyStack", "push", "push", "top", "pop", "empty"], values = [null, 1, 2, null, null, null]
Output: [null, null, null, 2, 2, false]
💡 Note: Create stack, push 1, push 2, top returns 2 (last pushed), pop returns 2, stack not empty (contains 1)
Example 2 — Single Element
$ Input: operations = ["MyStack", "push", "pop", "empty"], values = [null, 5, null, null]
Output: [null, null, 5, true]
💡 Note: Create stack, push 5, pop returns 5, stack is now empty
Example 3 — Multiple Operations
$ Input: operations = ["MyStack", "push", "push", "push", "top", "pop", "top"], values = [null, 1, 2, 3, null, null, null]
Output: [null, null, null, null, 3, 3, 2]
💡 Note: Push 1,2,3; top shows 3 (most recent); pop removes 3; top now shows 2

Constraints

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

Visualization

Tap to expand
Implement Stack using Queues Two Queue Approach (Pop Heavy) INPUT Operations: ["MyStack","push","push","top","pop","empty"] Values: [null, 1, 2, null, null, null] Two Queues Structure: Queue1 (main): 1 2 ... front Queue2 (helper): empty Stack: LIFO (Last In First Out) Queue: FIFO (First In First Out) ALGORITHM STEPS 1 Push Operation Add element to back of Q1 q1.push(x) // O(1) 2 Pop Operation Move n-1 elements Q1 to Q2 Pop last from Q1, swap queues 3 Top Operation Same as pop but keep element Transfer back after reading 4 Empty Check Return Q1.isEmpty() Pop Example (stack has [1,2]): Q1: 1 2 Q2: 1 pop: 2 swap Q1,Q2 FINAL RESULT Execution Trace: MyStack() --> null push(1) --> null [1] push(2) --> null [1,2] top() --> 2 [1,2] pop() --> 2 [1] empty() --> false [1] Output: [null, null, null, 2, 2, false] Complexity Analysis Push: O(1) time Pop/Top: O(n) time Empty: O(1) time Space: O(n) for two queues Key Insight: To simulate LIFO with FIFO queues, we need to reverse element order during pop. Transfer n-1 elements to Q2, pop the last element (stack top), then swap queues. This makes pop O(n) but push O(1) - "Pop Heavy" approach. Alternative: Make push O(n) and pop O(1) - "Push Heavy" approach. TutorialsPoint - Implement Stack using Queues | Two Queue Approach (Pop Heavy)
Asked in
Bloomberg 15 Microsoft 12 Amazon 10 Apple 8
89.5K Views
Medium Frequency
~15 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