Stack coding interview questions
Stack for matching and nesting
Push the most recent unmatched item, and pop when the new item resolves it.
1.1 Valid Parentheses Easy 3 min
I scan the string and push every opening bracket onto a stack. A closing bracket must match the most recent opening one, so I pop and compare. If the stack is empty when I need to pop, or a pair does not match, or anything is left on the stack at the end, the string is invalid.
Read the full answer1.2 Asteroid Collision Medium 3 min
Positive asteroids move right and negative ones move left, so a collision happens only when a right-moving one is immediately followed by a left-moving one. I push asteroids onto a stack, and while the new one is moving left and the top is moving right I resolve the fight: the smaller absolute value explodes, and equal sizes explode both. What remains on the stack is the final state, left to right.
Read the full answer1.3 Minimum Remove to Make Valid Parentheses Medium 3 min
I walk the string and remember the indices of unmatched opening parentheses on a stack. An unmatched closer is marked for deletion immediately, and anything still on the stack at the end is an unmatched opener, also marked. I then rebuild the string without those indices. Letters are copied through unchanged.
Read the full answer
Augmented stack
Store an extra running value with each entry so getMin stays constant time.
2.1 Min Stack Medium 3 min
I keep two stacks: one for the values and one for the minimum that was in force after each push. Push records the new value and min(value, current min). Pop, top and getMin then all run in constant time, because the current minimum is always the top of the second stack.
Read the full answer
Monotonic stack
Keep the stack increasing or decreasing so the top is the previous unresolved candidate for next greater, next smaller, or a greedy digit.
3.1 Next Greater Element I Easy 3 min
nums1 is a subset of nums2, and for each value in nums1 I need the first larger value to its right in nums2, or -1. I scan nums2 once with a decreasing stack: when a new value is larger than the top, it is the next greater of that top, and I record it in a map. Then I look up each nums1 value in the map.
Read the full answer3.2 Daily Temperatures Medium 3 min
For each day I need the number of days until a warmer one, or zero if none exists. I keep a stack of indices whose temperatures are decreasing and still unresolved. When a warmer day arrives I pop, and the difference of indices is the wait. Days left on the stack stay at zero.
Read the full answer3.3 Remove K Digits Medium 4 min
To make the smallest possible number after deleting k digits, I want earlier digits to be as small as possible. I build an increasing stack of digits and, while the new digit is smaller than the top and I still have deletions left, I pop the top. I then drop any leftover deletions from the end, strip leading zeros, and return "0" if nothing remains.
Read the full answer3.4 Largest Rectangle in Histogram Hard 3 min
The largest rectangle that uses bar i as its shortest bar extends left and right until a strictly shorter bar. A monotonic increasing stack of indices tells me those bounds: when a shorter bar arrives, the popped bar's right edge is the new index and its left edge is the new top. A sentinel of height 0 at the end flushes the stack. Each bar is pushed and popped once.
Read the full answer