Pattern #15

Stack

Important interview questions, thinking patterns, monotonic stack techniques, and rules for solving stack problems in Go.

Must Solve

15 core questions — solve these first.

  1. 1.Valid Parentheses
    easy
  2. 2.Min Stack
    medium
  3. 3.Evaluate Reverse Polish Notation
    medium
  4. 4.Daily Temperatures
    medium
  5. 5.Next Greater Element I
    easy
  6. 6.Next Greater Element II
    medium
  7. 7.Largest Rectangle in Histogram
    hard
  8. 8.Car Fleet
    medium
  9. 9.Decode String
    medium
  10. 10.Remove K Digits
    medium
  11. 11.Asteroid Collision
    medium
  12. 12.Simplify Path
    medium
  13. 13.Basic Calculator
    hard
  14. 14.Basic Calculator II
    medium
  15. 15.Score of Parentheses
    medium

Also Important

10 more questions worth practicing.

  1. 16.Backspace String Compare
    easy
  2. 17.Baseball Game
    easy
  3. 18.Remove All Adjacent Duplicates In String
    easy
  4. 19.Make The String Great
    easy
  5. 20.Online Stock Span
    medium
  6. 21.Exclusive Time of Functions
    medium
  7. 22.Validate Stack Sequences
    medium
  8. 23.Remove Duplicate Letters
    medium
  9. 24.Sum of Subarray Minimums
    medium
  10. 25.Maximum Width Ramp
    medium

How to Think

  1. Need last item first?Stack (LIFO)
  2. Brackets / parentheses?Stack
  3. Need undo previous thing?Stack
  4. Need next greater / smaller?Monotonic Stack
  5. Need process nested structure (3[a2[c]])?Stack
  6. Need evaluate math expression?Stack

Go Slice as Stack Template

Basic Stack Operations in Go

stack := []int{}

// Push
stack = append(stack, val)

// Top (Peek)
top := stack[len(stack)-1]

// Pop
top = stack[len(stack)-1]
stack = stack[:len(stack)-1]

// Check Empty
isEmpty := len(stack) == 0

👉 Push / Pop / Top are all O(1) time complexity!

Stack vs Queue

Stack (LIFO)
Last In → First Out
Analogy: Stack of plates

Top item is pushed last and popped first.

Queue (FIFO)
First In → First Out
Analogy: People standing in line

Front item is pushed first and popped first.

Visual Memory Rule
Parentheses   → Open: Push | Close: Match & Pop Top
Next Greater  → Monotonic Stack (while current > top -> pop)
Distance      → Store INDEXES on stack (i - stack.Top)

💡 Golden Rule: "If the newest unfinished thing should be handled first, think Stack."

Common Interview Mistakes

1. Popping Empty Stack

Accessing stack[len(stack)-1] without checking len(stack) > 0 causes array index out-of-range panic.

2. Storing Value Instead of Index

For problems like Daily Temperatures where distance matters, store indexes on the stack, not just values.

3. Forgetting Multiple Pops

One new large element can resolve multiple smaller unresolved elements. Use a while len(stack) > 0 && current > top loop!

4. Using Stack When Queue Is Needed

If you need the oldest item processed first, that's a Queue (FIFO), not a Stack (LIFO).

Interview Rules

  1. 1. Parentheses? → Stack
  2. 2. Undo / Backtracking state? → Stack
  3. 3. Nested expression? → Stack
  4. 4. Evaluate math expression? → Stack
  5. 5. Need last unresolved item? → Stack
  6. 6. Next greater/smaller? → Monotonic Stack
  7. 7. Distance to next greater? → Store indexes on stack
  8. 8. Push / Pop / PeekO(1) time complexity
  9. 9. In Go → Use slice as stack (append and slice truncation)

Small Rules

  1. Rule 1: Main operations Push, Pop, Top/Peek are O(1).
  2. Rule 2: Stack processes items in reverse order (LIFO).
  3. Rule 3: Before popping, always check len(stack) > 0.
  4. Rule 4: For matching brackets, top must match the current closing bracket type.
  5. Rule 5: Store indexes instead of values when distance/position is needed (e.g. Daily Temperatures).

Production Thinking

Undo / RedoActions pushed onto stack, undo pops latest action

Browser NavigationBack button history behaves like a stack of visited URLs

Function Call StackLIFO call frames (main -> foo -> bar)

Compilers / ParsersNested syntax trees, brackets, and math expressions

Path ProcessingDirectory traversal `/a/b/../c` (encounter `..` -> pop)

Remember This

Last In            → First Out
Push               → Add top
Pop                → Remove top
Parentheses        → Stack
Nested data        → Stack
Undo               → Stack
Next greater       → Monotonic Stack
Need distance      → Store index

💡 Golden Rule: "If the newest unfinished thing should be handled first, think Stack."