Best on a larger screen — the animation and controls have more room to breathe.
Inputs

Stack

A stack is a last-in-first-out (LIFO) data structure with two core operations: push adds an element to the top, and pop removes the most recently added one. Every operation touches a single end (with no shifting, scanning, or middle access), so on this lesson's Python list both run in amortized O(1) time.

A stack of plates in a cafeteria works the same way: clean plates go onto the top, and the next plate taken comes off the top. That single-end rule fits tasks where a program must pause work and resume it in reverse order: function calls, undo history, matching nested brackets, and depth-first traversal.

How a Stack Works

The displayed code implements a bounded stack: a Python list data plus a max capacity. push(val) first checks len(self.data) >= self.max: if the stack is full, it raises a Stack Overflow exception; otherwise it appends val to the end of the list. pop() checks for emptiness (popping an empty stack raises Stack Underflow) and otherwise removes and returns the last element.

The "top" of the stack is the last index of the list. append and list.pop() both operate at that end, which is why each costs O(1) amortized. Because operations never reach below the top element, their cost does not depend on how many items sit underneath.

Overflow is part of this lesson's code (embedded systems use bounded stacks as well), though a fixed bound is not required by the LIFO contract itself; a stack backed by a plain Python list grows until memory runs out. The capacity slider's 3–10 range is only a visualization limit because the canvas fits about ten cells.

Stack Operations Step by Step

The lesson starts from the default state: an empty stack with capacity 8. Drive it with the Push and Pop buttons to reproduce the trace below.

  1. push(3): guard passes (0 < 8), stack becomes [3]. Top index is 0.
  2. push(7): stack becomes [3, 7]. The new cell lands above the old top.
  3. push(1): stack becomes [3, 7, 1].
  4. pop(): returns 1, the last value in. Stack is [3, 7]. Note the reversal: last in, first out.
  5. pop(): returns 7. Stack is [3].
  6. push(9): stack becomes [3, 9]. The slot 7 vacated is reused.
  7. pop() twice: returns 9, then 3. The stack is empty again; one more pop() raises underflow.

Triggering the opposite guard once clarifies overflow: a ninth push into the capacity-8 stack raises overflow before appending, leaving the stack unchanged on a failed operation.

In the player, the stack renders as a vertical container with walls on three sides and an open top, so pushes drop in and pops lift out through the top. The header tracks size and the current top value, hiding the top value while the stack is empty so stale state is never displayed.

Time and Space Complexity

OperationTimeSpace
pushO(1) amortizedO(1) per element
popO(1) amortized—
peekO(1)—
Total storage—O(n)

The top-only interface paired with a list that appends and pops at its end yields these costs. A growable list occasionally resizes and copies every element, which is why push is amortized rather than worst-case O(1).

Edge Cases

  • Push to a full stack: raises overflow; the stack is unchanged.
  • Pop from an empty stack: raises underflow. Guard with an emptiness check or catch the exception.
  • Single element: push then pop round-trips the same value; size goes 0 → 1 → 0.
  • Duplicates: valid. A stack is not a set; it records order rather than membership.

Common Mistakes

  • Confusing pop with peek. pop mutates the stack. Calling it to inspect the top removes the element unless you store the return value; read non-destructively with data[-1].
  • Pushing and popping at different ends. Push at the back and pop from the front, and you have built a queue instead of a stack.
  • Skipping the empty check. Python's list.pop() raises IndexError on an empty list; JavaScript's Array.pop() returns undefined, a bug that often surfaces far from the pop site. This lesson's pop checks first and raises Stack Underflow.
  • Indexing into the middle. Once callers read data[k], the LIFO invariant no longer holds; use a list or deque instead.
  • Not noticing the call stack. Every function call is a push, and every return is a pop. The "stack overflow" hit by runaway recursion is this same overflow condition at runtime scale.