<aside>

This section is part of the 5-hour LeetCode DSA Patterns course by Mukul Raina

SLIDE 45: Section Intro, Stack and Monotonic Stack

Section 4: Stack and Monotonic Stack

In Sections 2 and 3, every pattern processed elements in a single left-to-right pass with decisions made immediately. Stacks introduce a different idea: sometimes you cannot make a decision about an element right away. You need to wait until you see what comes later. The stack holds unresolved elements, and each new element either gets pushed (deferred) or triggers a resolution of elements already on the stack.

This is a fundamentally different way of thinking about array problems. Instead of “process each element as you see it,” the mental model becomes “defer elements until you have enough information to resolve them.”

What We’ll Cover:

  1. Stack fundamentals and LIFO reasoning
  2. Stack for matching and nested structures
  3. Stack for state tracking
  4. Monotonic stack concept and invariant
  5. Next greater/smaller element pattern
  6. Values vs indices: what the stack should store

Where This Appears in Interviews:

Company Type How This Section Appears
FAANG / Big Tech Valid Parentheses as warm-up, Daily Temperatures and Largest Rectangle in Histogram as main problems
Mid-size companies Parenthesis matching, expression evaluation, and basic monotonic stack problems
Startups Parsing nested structures, processing event logs with undo operations
Online assessments Next greater element and stock span problems are common timed questions

Core Idea for This Section: Use a stack when the most recent unresolved item matters. Use a monotonic stack when each element needs to know the next greater or next smaller element.

<aside>

Mukul Raina | youtube.com/@mukulraina | slides.mukulraina.com | LinkedIn

</aside>


SLIDE 46: Stack Fundamentals, LIFO and When It Matters

What a Stack Is: A stack is a collection where the last element added is the first element removed (Last In, First Out). You can only interact with the top: push adds to the top, pop removes from the top, peek reads the top without removing.