Stacks and queues: deferring a decision
A stack is not a container, it is a policy — "I can't decide about this item yet, so hold it until I can" — and once you read it that way, parentheses, expression evaluation, undo, and recursion are all one problem; queues are the mirror image, with one trap that costs real interviews, because the obvious array implementation of a queue is quadratic.
What you'll be able to do
- Describe a stack as a set of deferred obligations and use that reading to solve matching and evaluation problems
- Design a min stack in O(1) per operation and compare the two standard designs by their space
- Explain why `Array.prototype.shift` (or `list.pop(0)`) makes a queue O(n) per dequeue, and implement two O(1) alternatives
- Prove the amortised O(1) bound for the two-stack queue by counting each element's total handling
- Convert a recursive function into an explicit-stack loop, and say when that is worth doing
The rest of this lesson is in the app
A stack is not a container, it is a policy — "I can't decide about this item yet, so hold it until I can" — and once you read it that way, parentheses, expression evaluation, undo, and recursion are all one problem; queues are the mirror image, with one trap that costs real interviews, because the obvious array implementation of a queue is quadratic. This walkthrough runs about 24 minutes, with runnable code you can edit and re-run as you read.
Continue in ChannelPulseThe first module of every track is free to read on the web — see what's open in Coding & data structures.