Stack

61 articles

dsa16 min read

Daily Temperatures [Medium] — Monotonic Stack Masterclass [Amazon, Google]

LeetCode 739 — Find the number of days until a warmer temperature for each day using a monotonic decreasing stack. The key insight: store indices, not temperatures, in the stack. When a warmer day arrives, it is the "next greater element" for every cooler day still waiting on the stack — a fundamental pattern that appears in at least six other LeetCode problems.

Read →
dsa17 min read

Largest Rectangle in Histogram [Hard] — Monotonic Stack Deep Dive [Amazon, Google]

Master LeetCode 84 the right way. Learn exactly why bars are popped from the monotonic stack, how to calculate left/right boundaries without bugs, the sentinel-values trick that eliminates edge cases, and a step-by-step dry run on [2,1,5,6,2,3] — plus how this pattern extends directly to Maximal Rectangle (LC 85).

Read →
dsa2 min read

Binary Search Tree Iterator

Implement an in-order BST iterator with O(h) space using a stack to simulate recursive in-order traversal.

Read →