LIFO and FIFO structures powering monotonic stacks, expression evaluation, and windowed queues.
Build custom structures from stacks and queues
When to reach for it
Spot it in the prompt
Look for problems that require you to design a stack-based system or implement specific functionalities.
How to solve it
Animation: Design Problems techniqueUse two stacks: push onto an input stack, and transfer to an output stack for O(1) amortized dequeue and peek.
Open full solutionFind nearest greater or smaller in one pass
O(n)naïve O(n^2)When to reach for it
Spot it in the prompt
Look for problems where you need to maintain a monotonic increasing or decreasing stack to efficiently track elements' positions or values.
How to solve it
Use a decreasing monotonic stack of indices; pop while the current temperature is warmer and record the day difference.
Open full solutionParse and compute expressions with stacks
When to reach for it
Spot it in the prompt
Look for problems that involve evaluating arithmetic expressions or postfix notation.
How to solve it
Animation: Expression Evaluation techniqueScan the string, applying multiplication and division immediately while pushing additive terms on a stack, then sum the stack.
Open full solutionPair two stacks to track extra state
When to reach for it
Spot it in the prompt
Look for problems where you can simulate a specific behavior or implement operations efficiently by employing two stacks.
How to solve it
Animation: Two Stacks techniquePush operands on a stack; on each operator pop two operands, apply it, and push the result back.
Open full solutionTrack window extremes in amortized constant time
O(n)naïve O(n*k)When to reach for it
Spot it in the prompt
Look for problems where you need to process elements in a sliding window or maintain monotonicity in a queue, especially in array-related or streaming problems.
How to solve it
Animation: Sliding Window & Monotonic Queue techniqueMaintain a decreasing deque of indices; pop smaller values from the back and out-of-window indices from the front so the front stays maximal.
Open full solutionFDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI