Last-in, first-out data structure. Learn push/pop and monotonic stack patterns.
1 algorithm2 variants8 problems
3 visualizers. Each one stands alone.
A stack hands back the newest item first. That one rule explains the call stack in every running program, undo in an editor, the back button, and depth-first search. In interviews a stack is usually hidden inside the question: anything about nesting, matching pairs, or the nearest bigger or smaller neighbour tends to be a stack problem. This section has the structure itself and the two patterns that come up most.
Only the top is ever touched, so whatever sits on top is the most recent thing still waiting to be dealt with. Push, pop and peek each cost O(1) because nothing below the top moves. In both problem pages the stack holds unfinished business: openers still waiting for a closer, or positions still waiting for a bigger value. When the awaited thing arrives it deals with the top of the stack, and on the monotonic page it keeps popping for as long as the top is smaller.
If the answer depends on matching the most recent open item, it is the parentheses pattern. If it depends on comparing sizes with the nearest neighbour, it is the monotonic pattern, and the usual slip there is pushing values instead of indices, which leaves you no slot to write the answer into. Both pages run in linear time because every item is pushed once and popped at most once.