L2 · Working engineerStacks & queues~8 min · 4 tests#46

Implement a Stack Using Queues

Build a LIFO stack using only FIFO queue operations (deque append/popleft). Compare push-heavy and pop-heavy designs and their O(n) trade-off.

The problem

Implement MyStack using only queue operations on collections.deque: append (to the back), popleft (from the front) and len.

  • push(x)
  • pop() — returns the top, or None if empty

Examples

  1. Example 1

    Input

    run_ops(MyStack, [], [["push", 10], ["push", 20], ["push", 30], ["pop"]])

    Expected output

    [None, None, None, 30]
  2. Example 2

    Input

    run_ops(MyStack, [], [["push", 1], ["push", 2], ["pop"], ["pop"], ["pop"]])

    Expected output

    [None, None, 2, 1, None]

+ 2 hidden tests on Submit.

Edge cases to ask about

  • Pop from empty
  • Interleaved operations

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · MyStack
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and read why.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Costly push (rotate new item to front)O(n) push, O(1) popO(n)One queue is enough.
    bestCostly pop (move n−1 items to a second queue)O(1) push, O(n) popO(n)The classic two-queue version.
    Walkthrough of the optimal approach (try it yourself first)

    After appending a new item, rotate the queue len − 1 times (popleft + append) so the new item sits at the front. Then pop is a plain popleft.

    Push is O(n) and pop is O(1). The two-queue version flips that trade-off — the interviewer may ask which you would choose for a push-heavy workload.

    Complexity: O(n) time, O(n) space. With the rotate-on-push design, push re-queues the n − 1 older items; pop is O(1).

    Reveal the reference solution
    from collections import deque
    
    class MyStack:
        def __init__(self):
            self.q = deque()
    
        def push(self, x):
            self.q.append(x)
            for _ in range(len(self.q) - 1):     # rotate x to the front
                self.q.append(self.q.popleft())
    
        def pop(self):
            return self.q.popleft() if self.q else None

    Follow-ups interviewers ask

    • Make push O(1) and pop O(n) instead.
    • Add top().

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Implement a Stack Using Queues in Python?

    The optimal solution runs in O(n) time and O(n) auxiliary space. With the rotate-on-push design, push re-queues the n − 1 older items; pop is O(1).

    What is the brute-force approach, and how do you optimise it?

    Costly push (rotate new item to front): O(n) push, O(1) pop time, O(n) space. One queue is enough. Costly pop (move n−1 items to a second queue): O(1) push, O(n) pop time, O(n) space. The classic two-queue version.

    What follow-up questions do interviewers ask about Implement a Stack Using Queues?

    Make push O(1) and pop O(n) instead. Add top().