π JavaScript Stack & Queue β Topic Index
Stacks (LIFO) and Queues (FIFO) are foundational data structures powering parsing, monotonic algorithms, BFS, and sliding window patterns.
Topics Covered
| # | Topic | Key Concepts |
|---|---|---|
| 1 | Basics | Array as stack/queue, deque patterns |
| 2 | Patterns | Monotonic stack, deque, two-stack tricks |
| 3 | Interview Questions | 25 classic problems |
When to Use Stack
- Matching brackets / balanced parentheses
- Monotonic next-greater / next-smaller problems
- DFS traversal (explicit stack)
- Undo/redo, history navigation
When to Use Queue
- BFS level-order traversal
- Sliding window maximum (deque)
- Rate limiting / scheduling
- Implementing other data structures
JavaScript Data Structures
// Stack (LIFO) β use Array
const stack = [];
stack.push(1); // push O(1)
const top = stack.at(-1); // peek O(1) β modern JS
const val = stack.pop(); // pop O(1)
// Queue (FIFO) β use Array (shift is O(n) β fine for interviews)
const queue = [];
queue.push(1); // enqueue O(1)
const front = queue[0]; // peek O(1)
const v = queue.shift(); // dequeue O(n) β acceptable in interviews
// Deque β both ends O(1) via two-array trick or splice
// For performance: use custom DLL or @datastructures-js/deque