Implement Stack Using Queues
Checking your account…
Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace
Problem
Implement MyStack with push, pop, top, and empty using queue operations. Rotate the queue after each push so its front is the stack top.
Starter code
class MyStack:
def __init__(self):
passTest cases
basic
{
"operations": [
"MyStack",
"push",
"push",
"top",
"pop",
"empty"
],
"arguments": [
[],
[
1
],
[
2
],
[],
[],
[]
]
}Expected: [null,null,null,2,2,false]
Wizard outline
- Step 1: Initialize MyStack
Replace the empty starter with the first real state owned by MyStack. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.
- Step 2: Assemble the primary transition
Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.
- Step 3: Pass the Wizard Stateful Stack Top case
Complete the readable core algorithm for one representative Interview case. One rotation after append converts FIFO storage into the required LIFO access order.
- Step 4: Harden the Wizard Stateful Stack Pop Transition boundary
Repair the reviewed boundary and pass the complete submission contract. Because push already restored LIFO order, popleft is both the returned top and the required state mutation.
Footguns and prerequisites
- Using list.pop() directly implements a stack and bypasses the queue-only constraint.
- python specific rapid fire
Reviewed references
Practice prerequisites
- Advance a Circular Buffer(opens in a new tab)
Advance a Circular Buffer isolates head identifies the oldest live value, tail identifies the next write slot, and size stays between zero and capacity. That focused state discipline is required when implementing implement stack using queues as a complete Interview Problem.
- Apply One Stack Reduction(opens in a new tab)
Apply One Stack Reduction isolates the stack is the fully reduced form of the processed prefix and contains no adjacent equal pair. That focused state discipline is required when implementing implement stack using queues as a complete Interview Problem.
Recommended approach and implementation
Store one deque. Append the new value, then rotate every earlier element from the front to the back so the new value becomes the front.
Why it works: After each push, queue order is newest to oldest. Therefore popleft removes the most recent value, the front is the stack top, and the invariant is restored after every rotation.
class MyStack:
"""
Checkpoint 1: initialize the state owned by this Interview contract.
Checkpoint 2: assemble the primary transition without hiding the boundary.
"""
def __init__(self):
from collections import deque
self.queue = deque()
def push(self, value):
self.queue.append(value)
for _ in range(len(self.queue) - 1):
self.queue.append(self.queue.popleft())
def pop(self):
return self.queue.popleft()
def top(self):
return self.queue[0]
def empty(self):
return not self.queue