Module F-6·16 min read

LIFO structure, the call stack, and balanced-bracket and expression-parsing patterns.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

Module F-6 — Stacks: LIFO, the Call Stack, and Expression Parsing

What this module covers: A stack is one of the simplest structures in this course — and it's already running underneath every function call you've ever written, whether you knew it or not. This module covers the stack as an abstract data type, how it's built on top of the array from Module F-2, the call stack that governs recursion and stack overflows, and the balanced-bracket and expression-parsing patterns a stack solves cleanly.


The Stack: Last-In, First-Out

A stack supports exactly three operations, all O(1): push (add to the top), pop (remove from the top), and peek (look at the top without removing it). You can only ever interact with the most recently added item — everything below it is inaccessible until the items above it are popped off.

typescript

Notice this is just a thin wrapper around the array from Module F-2 — a stack isn't a new memory layout, it's a discipline about which operations you allow. Restricting yourself to push/pop/peek at one end is precisely what keeps every operation O(1): those are exactly the array operations that never require shifting other elements.

Analogy: A stack is a pile of plates. You put a new plate on top (push), and you take the top plate off (pop). You cannot pull a plate from the middle without first removing everything stacked above it — the same constraint the data structure enforces.


The Call Stack: Where Your Functions Actually Live

Every time you call a function, the runtime pushes a stack frame — the function's local variables, its arguments, and the address to return to — onto the call stack. When the function returns, its frame is popped, and execution resumes exactly where the caller left off.

typescript

This is a real stack, following the exact same LIFO rule as the Stack class above — the last function called is the first one to return, because it's sitting on top of everyone waiting on it. This is also why deep, unbounded recursion causes a stack overflow: each recursive call pushes another frame, and if the recursion never reaches its base case, frames pile up until the call stack — a fixed-size region of memory — runs out of room.

typescript

Module P-1 (Recursion and the Call Stack) goes deep on this — how the call stack grows and shrinks with correct, base-cased recursion, and how to reason about how many frames a given recursive function will actually push.


Balanced Parentheses

The single most common interview and real-world use of a stack: checking whether brackets in an expression are properly nested and closed.

typescript

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.