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

Lesson 6.3 of 10 in this module Unrolled Pro

Implement Queue using Stacks

Build a first-in-first-out queue from two last-in-first-out stacks. From one stack that is emptied and refilled on every pop (O(n) per pop), to an inbox and an outbox with a lazy transfer that moves each element at most once (O(1) amortised), with the amortised argument made concrete by counting moves. Then a latency limit that rules out the occasional O(n) pop, a fixed-capacity ring buffer, a stack from queues, and a bounded prefetch queue between threads.

about 10 min 5 Python examples 2 quick checks

Problem. Implement a FIFO queue with push(x), pop(), peek() and empty(), using only stack operations: push to the top, pop from the top, look at the top, size, and is-empty.

text
push(1), push(2), peek() → 1, pop() → 1, empty() → False, pop() → 2, empty() → True

The point isn't that you'd do this in Python (you'd use deque). It's the cleanest example of amortised cost: an operation that is occasionally expensive but cheap on average, and of how reversing twice restores order.

ML context. Queues sit between every producer and consumer in an ML system: a data loader filling batches for the GPU, a model server holding requests, a replay buffer of recent experience. Their two engineering questions are the ones this lesson ends on: what does the worst single operation cost (a latency spike) and what happens when the buffer is full (Changes A, B and D).


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 "Implement Queue using Stacks"

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