Foundations
Linear Structures
Hashing
Array and String Patterns
Recursion, Search, and Sorting
Problem Solving Patterns
Algorithm Searching
Sorting Algorithms
Array Algorithms
Grid Algorithms
Linear Structure Algorithms
Hashing Algorithms
Recursion and Backtracking
Greedy Algorithms
Tree and Heap Algorithms
Graph Algorithms
Dynamic Programming Algorithms
String Algorithms
Math Algorithms
Keyboard ready
Space plays. Arrow keys step. R restarts.
LAB Stacks Lab
Linear Data Structures
Concept shape
Core
0
1
2
3
4
5
Definition
A stack is a last-in-first-out structure: the newest item is removed first.
Step 1 / 5
Concept shape
Linear Data Structures Core
Stacks
Welcome to the Stacks concept lab. A stack keeps the newest item at the top, and only the top is meant to be removed next. This is useful when the most recent unfinished work should be handled first, such as function calls, undo history, nested brackets, or depth-first exploration. If the oldest item must leave first, use a queue instead. The must know points are: Push adds to top. Pop removes from top. Call stacks and undo flows behave like stacks. The common operations are: push O(1), pop O(1), peek O(1), isEmpty O(1). The complexity shape is Core operations O(1). Watch out for these pitfalls: Popping from an empty stack. Using a stack when FIFO order is required. Related algorithms include DFS, Valid Parentheses, Monotonic Stack.
Concept details
A stack keeps the newest item at the top, and only the top is meant to be removed next.
This is useful when the most recent unfinished work should be handled first, such as function calls, undo history, nested brackets, or depth-first exploration.
If the oldest item must leave first, use a queue instead.
Must know
- Push adds to top.
- Pop removes from top.
- Call stacks and undo flows behave like stacks.
Complexity
Core operations O(1)
1.0x