Skip to main content
All Data Structures courses

Stack

The data structure behind recursion, memory management, and much more

39 Lessons
37 problems
5 Patterns
168 Illustrations
44 Slides

What you will learn

How LIFO ordering powers browser back, editor undo, and the processor's call stack

Two stack implementations: a bounded array-backed stack and an unbounded linked-list-backed stack

Infix, postfix, and prefix notation, and how to evaluate and convert between them

The monotonic stack idiom for previous and next greater or smaller element problems

Sequence validation and linear evaluation patterns for parentheses, paths, and bracketed expressions

How to design a min stack and a max stack that return their extreme value in O(1)

Why this course

push and pop are the easy half. The harder half is recognising that the next greater element, the largest rectangle in a histogram, trapped rainwater, and the stock span problem are the same monotonic-stack pattern wearing four different costumes. The clue in every one of them is the same single word: monotonic. Once you see it, the solution is six lines of code on top of a data structure you already know.

This course rebuilds your understanding of the stack from LIFO ordering up, then walks you through the five problem-solving patterns that cover almost every stack question asked in a coding interview. You finish by designing a min stack and a max stack that report their extreme value in O(1).

Requirements

The course assumes you can read code in any mainstream language and have seen an array before, but no prior stack experience is needed.

  • Comfort writing loops, conditionals, and functions in at least one language.
  • Familiarity with arrays and basic Big-O notation (O(1), O(N), O(N log N)).
  • Optional: prior exposure to linked lists, which the second implementation chapter relies on.

Overview

A stack is the data structure your processor already uses to make function calls work. Every recursive program, every return statement, every backtracking search depends on a stack frame somewhere in memory. The same LIFO discipline that runs your call stack also collapses several otherwise quadratic problems, like next greater element and largest rectangle in histogram, into clean O(N) scans over the input.

Representation of a stack

This course covers both halves of the problem: how the data structure actually works, and how to recognise which of the five standard patterns a given problem fits into.

Fundamentals

The course opens with the LIFO insight itself. You see why browsers, text editors, and the processor's call stack all need a structure that hands back the most recent insertion first, and how restricting access to a single end turns push, pop, top, and size into O(1) operations.

From there you build the stack twice. The first implementation uses a fixed-capacity array with a topIndex field, bounded behaviour, and constant-time push and pop. The second uses a singly linked list with the head node as the top, giving you an unbounded stack at the cost of one allocation per push. Both implementations expose the same four operations, and the side-by-side comparison makes the time and space trade-offs concrete.

The fundamentals close with expression notation. You learn why infix is what humans read but a chore to evaluate, why postfix and prefix drop precedence and parentheses entirely, and how a single stack evaluates a postfix or prefix expression in O(N). The same machinery powers six conversion lessons covering every direction between the three notations.

Problem solving

After the fundamentals, the rest of the course is patterns. Most stack problems you will see in an interview reduce to one of five templates. Spot the template on first read, and the push, pop, and update rules fall out almost mechanically.

The 5 stack patterns you'll master
Reversal
Push every item onto a stack and pop them off to retrieve the sequence in opposite order, in O(N) time and O(N) space. Stack Inversion, Reverse The String, Reverse An Array, Reverse Word Order.
Previous closest occurrence
A monotonic stack pass that finds the previous greater or smaller element for each index in a single O(N) forward scan over the sequence. Preceding Superior Element, Preceding Inferior Element, Preceding Superior Element II, Preceding Inferior Element II.
Next closest occurrence
The mirrored monotonic stack idiom that finds the next greater or smaller element for each index, also in O(N) total work. Succeeding Superior Element, Succeeding Inferior Element, Succeeding Superior Element II, Succeeding Inferior Element II, Succeeding Superior Nodes, Retained Rainwater, Largest Rectangle Area.
Sequence validation
A stack of unclosed opening events that verifies a sequence against bracket-style rules in one O(N) scan, returning false the instant an invalid close is seen. Parentheses Checker, Minimum Edits, Redundant Parentheses, Balanced Span.
Linear evaluation
A stack that holds partially built data while one pass over the input applies trigger-specific add, update, and remove rules to the items on top. Canonicalise Path, Bracketed Reversal, String Expansion, Formula Parsing.

Each pattern is taught in three layers. First an Understanding lesson explains the technique on a problem where the pattern is obvious. Then an Identifying lesson teaches you the signals that tell you a new problem fits the template, the same triggers an experienced engineer spots on first read of the question. Then four to seven problems give you progressively harder applications. The course closes with a design capstone where you implement a min stack and a max stack, both returning their extreme value in O(1) by maintaining a second stack in sync with the main one.

How this course is different

Most stack material online either skips the implementation work or skips the monotonic patterns where stacks earn their reputation. This course commits to both.

Most DSA resources
This course
âś—Cover push and pop in one paragraph and jump straight to "100 stack problems".
✓Builds the stack twice, once on an array and once on a linked list, so the implementation trade-offs are concrete.
âś—Treat monotonic stack as one technique instead of two mirrored patterns, previous-closest and next-closest.
✓Splits the monotonic stack into previous-closest and next-closest as distinct patterns, each with its own identification signals.
âś—Skip expression notation entirely, leaving infix, postfix, and prefix as folklore from a CS class.
✓Spends three chapters on infix, postfix, and prefix evaluation and conversion before any pattern lesson begins.
âś—Show the parentheses checker once and never connect it to the four other problems built on the same opener-stack invariant.
✓Treats sequence validation as its own pattern with four problems all built on the same stack-of-openers invariant.
âś—Treat min stack as a clever trick instead of a design exercise grounded in the LIFO invariant.
✓Ends with a min stack and a max stack design capstone where you keep a second stack synchronised with the main one.

Who this course is for

The course suits anyone who needs to understand the stack as a tool, not just a vocabulary word.

  • New programmers who can call stack.push(x) but cannot explain why the call stack is a stack.
  • Self-taught and bootcamp graduates who solved the parentheses checker once and want to see why the same template solves four other problems.
  • Working developers who write recursive code daily but have never traced the call stack on paper.
  • Engineers preparing for FAANG and Big Tech interviews, where next greater element, largest rectangle in histogram, and trapping rainwater are standard onsite questions.
  • Returning engineers who learned postfix evaluation in school and want a refresher anchored in real interview problems.
  • Anyone who has tried to solve "Largest Rectangle in Histogram" by hand and walked away unsure what the stack was tracking.

Continue your DSA journey

The stack sits at the centre of several other topics in this curriculum. After this course, the natural next steps depend on which direction you want to go.

  • Array: Every problem in the previous-closest and next-closest patterns is an array problem, and the array-backed stack is the more efficient of the two implementations. The array course covers the eight patterns that stacks frequently work alongside, including the two-pointer family that the reversal pattern competes with.
  • Singly linked list: The linked-list-backed stack implementation in this course assumes you can already reason about node pointers and head insertion. If that chapter felt shaky, the singly linked list course covers the underlying mechanics in depth.
  • Queue: Stack and queue are the two restricted-access linear structures, LIFO and FIFO respectively. Studying them in sequence makes the difference between depth-first search (stack) and breadth-first search (queue) obvious once you reach the graph course.
  • Binary tree: Iterative inorder, preorder, and postorder traversal of a binary tree all use an explicit stack to simulate the call stack that recursion uses implicitly. The binary tree course has dedicated lessons on each iterative traversal where the patterns you learned here turn into working code.
  • Recursion: The call stack is itself a stack, so every recursion lesson is also a stack lesson in disguise. The recursion course teaches the call-stack frame discipline that explains why iterative tree traversal needs an explicit stack and how tail recursion can sometimes remove it.
  • Backtracking: Every backtracking search uses the call stack to remember choices it can undo. Understanding the stack first makes the backtracking course's three search patterns far less mysterious, because the "undo on the way back up" step is just a pop.

Frequently asked questions