L2 · Working engineerStacks & queues~6 min · 4 tests#41

Implement a Stack in Python

Build a Stack class with push, pop, peek, is_empty and size on a Python list, all O(1). Learn LIFO and why list.pop(0) would be the wrong end.

The problem

Implement a Stack (last in, first out):

  • push(x) adds to the top
  • pop() removes and returns the top, or None if empty
  • peek() returns the top without removing it, or None
  • is_empty() and size()

Examples

  1. Example 1

    Input

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

    Expected output

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

    Input

    run_ops(Stack, [], [["push", 1], ["peek"], ["size"], ["pop"], ["is_empty"]])

    Expected output

    [None, 1, 1, 1, True]

+ 2 hidden tests on Submit — empty stack.

Edge cases to ask about

  • Pop from empty
  • Peek on empty
  • Mixed types

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · Stack
    ⌘/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
    List, top at index 0O(n) push/popO(n)insert(0)/pop(0) shift every element.
    bestList, top at the endO(1) push/popO(n)append() and pop() work on the end.
    Walkthrough of the optimal approach (try it yourself first)

    Use a list and treat its end as the top: append to push, pop() to pop, [-1] to peek — all O(1). Using the front (insert(0, x), pop(0)) would be O(n) per operation, because every element shifts.

    Complexity: O(1) time, O(n) space. append() and pop() on the END of a Python list are amortised O(1); the stack stores n items.

    Reveal the reference solution
    class Stack:
        def __init__(self):
            self._items = []
    
        def push(self, x):
            self._items.append(x)
    
        def pop(self):
            return self._items.pop() if self._items else None
    
        def peek(self):
            return self._items[-1] if self._items else None
    
        def is_empty(self):
            return not self._items
    
        def size(self):
            return len(self._items)

    Follow-ups interviewers ask

    • Add get_min() in O(1) (min-stack).
    • Make it thread-safe.

    Frequently asked interview questions

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

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

    The optimal solution runs in O(1) time and O(n) auxiliary space. append() and pop() on the END of a Python list are amortised O(1); the stack stores n items.

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

    List, top at index 0: O(n) push/pop time, O(n) space. insert(0)/pop(0) shift every element. List, top at the end: O(1) push/pop time, O(n) space. append() and pop() work on the end.

    What follow-up questions do interviewers ask about Implement a Stack in Python?

    Add get_min() in O(1) (min-stack). Make it thread-safe.