18. Stack & Queue

Parentheses, expression evaluation, monotonic stack and deque.

🎬 Episodes 46–52: video coming soon
Your progress0 of 18
5easy
9medium
4hard

Understand the topic

A stack is last-in, first-out, like a pile of plates. A queue is first-in, first-out, like a line at a counter. A deque lets you add and remove at both ends. In Java, use ArrayDeque for all three; Stack and LinkedList are older and slower.

The big pattern here is the monotonic stack: keep the stack increasing (or decreasing), and every pop answers "next greater" or "next smaller" for the popped item. Each item is pushed and popped once, so it's O(n).

#ProblemPatternLevel
1 Valid Parentheses #20 Stack matching Easy LeetCode β†—
2 Implement Queue using Stacks #232 Two stacks Easy LeetCode β†—
3 Implement Stack using Queues #225 Rotate on push Easy LeetCode β†—
4 Next Greater Element I #496 Monotonic stack Easy LeetCode β†—
5 Backspace String Compare #844 Stack / backwards scan Easy LeetCode β†—
6 Min Stack #155 Stack of (value, min) pairs Medium LeetCode β†—
7 Evaluate Reverse Polish Notation #150 Operand stack Medium LeetCode β†—
8 Daily Temperatures #739 Monotonic stack of indexes Medium LeetCode β†—
9 Next Greater Element II #503 Monotonic stack, circular Medium LeetCode β†—
10 Online Stock Span #901 Monotonic stack with spans Medium LeetCode β†—
11 Asteroid Collision #735 Stack simulation Medium LeetCode β†—
12 Remove K Digits #402 Monotonic increasing stack Medium LeetCode β†—
13 Sum of Subarray Minimums #907 Monotonic stack contribution Medium LeetCode β†—
14 Design Circular Queue #622 Array ring buffer Medium LeetCode β†—
15 Largest Rectangle in Histogram #84 Monotonic stack Hard LeetCode β†—
16 Maximal Rectangle #85 Histogram per row Hard LeetCode β†—
17 Sliding Window Maximum #239 Monotonic deque Hard LeetCode β†—
18 Basic Calculator #224 Stack for parentheses and signs Hard LeetCode β†—

Work top to bottom β€” each list goes from easy to hard. Try a problem for at least 15 minutes before opening a hint. Ticks are saved in your browser.