learningto/pass

Stacks & Queues

EasyData Structures

Essential for parsing, BFS/DFS, and monotonic problems. The monotonic stack pattern solves a family of "next greater element" problems elegantly. Queues are the backbone of BFS.

AVG TIME
O(1)
SPACE
O(n)
BEST
O(1)
WORST
O(n)

Key Concepts

  • 1Monotonic stack maintains elements in sorted order by popping
  • 2Use deque (collections.deque) for O(1) both ends in Python
  • 3Stack for DFS, queue for BFS - the fundamental distinction
  • 4Valid parentheses and nested structure problems are classic stack use cases
  • 5Sliding window maximum uses a monotonic deque (O(n) total)

In Python

Concept to Code- 3 structures for this topic
import required:from collections import deque
declare
queue = deque()
common ops
queue.append(x)
O(1)
queue.popleft()
O(1)
len(queue)
O(1)
say in interview
"I'll use a queue for BFS - process nodes in the order we discover them"
Do NOT use list.pop(0) for a queue - that's O(n). Always use deque.popleft() which is O(1).

Math You Need For This

A can of Pringles (stack) or a roll of toilet paper dispenser (queue). Adding or removing one chip/sheet is always one action, regardless of how many are in there.

Required concepts
Key math ideas
1 / 2

Constant time - O(1)

No matter how large the input is, this operation always takes the same amount of time. The size of n simply does not matter.

Visual: A perfectly flat horizontal line on a graph. The line never rises no matter how far right you go.

Think of it this way: Looking up a word in a dictionary if you already know the exact page number. It does not matter how thick the dictionary is.

For algorithms: Accessing array[3] is O(1) because the computer calculates the memory address directly: start + 3 * item_size. One calculation, done.

Interactive 3D Visualization

Stack / Queue
Push: O(1)Pop: O(1)Space: O(n)
Drag to orbit - Scroll to zoom

Interactive Playground

Experiment hands-on before writing a single line in the practice editor. Try different inputs and watch the structure behave.

interactive sandbox

Data Structure Playground

TOP (last in, first out)
1TOP
9
2
7
4
BOTTOM
LIFO: Last In, First Out
Like a stack of plates. You can only add or remove from the top. Both push and pop are O(1).

Python Implementation

example.py
Loading...
Now try it yourself
2 challenges with test cases and AI feedback
Practice Now

Complexity Analysis

1481216nlog n1n (input size)
COMMON OPERATIONS
Array
Access:O(1)
Search:O(n)
Insert end:O(1)*
Insert mid:O(n)
Hash Map
Get/Set:O(1)*
Delete:O(1)*
Search value:O(n)
Iterate:O(n)
Linked List
Access:O(n)
Search:O(n)
Insert head:O(1)
Delete known:O(1)
Binary Search
Search:O(log n)
Insert:O(log n)
Balanced BST
Search:O(log n)
Insert:O(log n)
Delete:O(log n)
Heap
Peek min/max:O(1)
Push:O(log n)
Pop:O(log n)
Heapify:O(n)
Graph (BFS/DFS)
Traversal:O(V+E)
Dijkstra (heap):O((V+E) log V)
Merge / Quick Sort
Sort:O(n log n)
Space (merge):O(n)
Space (quick):O(log n)

* amortized average case

READY TO TEST YOURSELF?

Put Stacks & Queues into practice

The best way to lock in what you've learned is to write code. Solve real interview-style problems right now.

Next: Hash Tables
Stacks & Queues
Next: Hash Tables