VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 06: Stacks and queues

Lesson 6.5 of 10 in this module Unrolled Pro

Min Stack

A stack that also returns its minimum in O(1). From computing min() on every call (O(n)), to storing the minimum so far next to each element, to a second stack that only records new minimums (less memory when minimums are rare, with a duplicate trap). Then the minimum of a queue built from two min-stacks, which gives a rolling minimum of the loss in O(1) amortised, an O(1)-extra-space encoding for integers, and when you need a heap instead.

about 10 min 5 Python examples 2 quick checks

Problem. Design a stack with push(x), pop(), top() and get_min(), all in O(1) time. get_min() returns the smallest element currently in the stack.

text
push(-2), push(0), push(-3)
get_min() → -3
pop()
top()     → 0
get_min() → -2

The hard part is pop(): when the minimum leaves, the previous minimum must be available instantly, without looking through the stack.

ML context. A stack with "best so far" appears whenever you can roll back: a search over model configs where you try a change, undo it if it doesn't help, and always want the best validation loss among the current path; an undo history in a notebook tool; backtracking search (module 08). And two min-stacks make a min-queue, which gives the rolling minimum of a loss curve for plateau detection (Change B).


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Min Stack"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee