Pattern Studioalgorithmic thinking
All patterns

Stack

Stacks & Queues
Beginner
Interactive

Use last-in, first-out order to match nested delimiters.

Time
O(n)
Space
O(n)
ActiveComparingMatch / bestIn windowFrontierVisitedExcludedAnswer
No frames to display.

The idea

A stack exposes only its most recently pushed item. Balanced-parentheses checking uses that LIFO order to match every closing delimiter with the nearest unmatched opener.

Analogy

A hands-on model of stack that exposes every state change instead of hiding it inside a library call.

When to reach for it

  • Learning the underlying data structure.
  • Tracing state changes and invariants.
  • Building a reusable interview template.

Language-independent template

for char in text:
    if opener: push(char)
    else if pop() does not match: return false
return stack is empty

Complexity — time O(n), space O(n).

Common mistakes

  • Skipping boundary checks.
  • Updating state in the wrong order.
  • Using the structure without preserving its invariant.

Practice progression

Canonical interview problems, ordered from foundation to advanced application.

Easy

Check whether every delimiter closes in the correct order.

Medium

Evaluate a postfix expression with integer operands.

Hard

Evaluate an expression containing signs and nested parentheses.

Variations

Change the input and replay the trace.
Adapt the operation to a related interview problem.