Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
DATA STRUCTURES

Stacks

Last in, first out - the most recent item is always on top.

Beginner8 problems3 interactive subtopicsO(n) time · O(n) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (3)Open in full playground →
STEP 1/5Push (
push (
012345
(
[
]
{
}
)
ch
STACK - TOP AT THE TOP
(
  • active this step
  • visited / done
  • result
  • removed / popped
Space Play←→ StepR Replay
1 / 5
PYTHONs = "([]{})"
1def is_valid(s):
2 pairs = {")": "(", "]": "[", "}": "{"}
3 stack = []
4 for ch in s:
5 if ch in pairs:
6 if not stack or stack.pop() != pairs[ch]:
7 return False
8 else:
▶9 stack.append(ch)
10 return not stack
STEP 1 OF 5Setup

Push (

An opening bracket waits on the stack for its partner.

VARIABLES

ch
(
stack
["("]
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

A stack supports push (add to the top) and pop (remove from the top) in O(1). A Python list with append and pop works as a stack.

Why it matters

Many problems must match or undo the most recent thing first - brackets, function calls, browser history.

Think of it like this

A stack of plates: you add to and take from the top only.

Use it when

  • Matching pairs like brackets or tags
  • Undoing back to the latest state
  • Evaluating expressions

Avoid it when

  • You need first in, first out - use a queue
  • You need to search inside - a stack only exposes its top

Complexity cheatsheet

OperationTimeSpace
PushO(1) amortizedO(1)
Pop or peek the topO(1)O(1)
Search for a valueO(n)O(1)

Often confused with: monotonic stacks - a stack kept in sorted order to answer “next greater” questions.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

valid parenthesesnestedundomost recentevaluate expression
  • Input that “opens” and “closes” things maps to push and pop
  • Any recursion can be rewritten with an explicit stack
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Popping from an empty stack on input like ")("
  • Returning True without checking that the stack is empty at the end
  • Using pop(0) - that’s a queue operation and O(n)

Edge cases to test

  • Empty string - valid
  • Only opening brackets like "((("
  • A closing bracket first, like "]"
Interview tip: Say what the stack holds at any moment - “unmatched opening brackets” - it proves the code is correct.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

For the input "(]", where does the check fail?

06

Practice

Apply what you just watched on real interview problems.

Generate Stack problem in AI DSA Practice →
  • BeginnerValid ParenthesesLeetCode 20 ↗ (opens in a new tab)
  • IntermediateEvaluate Reverse Polish NotationLeetCode 150 ↗ (opens in a new tab)
  • AdvancedBasic CalculatorLeetCode 224 ↗ (opens in a new tab)
Linked ListsQueues