Practice SetLesson 091 min read

Monotonic Stack — Practice Problems

November 05, 2026

// How to use this set

Work these in order: easy (Valid Parentheses, Next Greater Element I) → medium (Min Stack, Evaluate RPN, Daily Temperatures, Next Greater Element II, Online Stock Span, Remove K Digits, Asteroid Collision) → hard (Largest Rectangle in Histogram, Trapping Rain Water via stack). The core move: keep the stack sorted and pop while the incoming element breaks the order — each pop resolves one 'next greater/smaller' answer in amortised O(1). Every solution below was run and tested.

Eleven problems that drill the stack and monotonic-stack pattern from Lesson 9. A plain stack handles matching and evaluation; a monotonic stack (kept increasing or decreasing) answers “next greater / next smaller / span” questions in a single O(n) pass. Each solution here was actually run and tested.

How to practise. For the monotonic ones, decide up front whether the stack should be increasing or decreasing, and whether you store values or indices (usually indices, so you can compute distances). Try each for ~20 minutes, then read the approach, close the page, and re-solve from memory the next day.

The 11 problems

  1. 1Valid Parentheses (LeetCode 20)EasyCheck that every bracket closes in the right order: push openers, and each closer must match the top.arrow_forward
  2. 2Next Greater Element I (LeetCode 496)EasyFor each query, find the next bigger value to its right in nums2: precompute all answers in one stack pass.arrow_forward
  3. 3Min Stack (LeetCode 155)MediumDesign a stack with O(1) getMin: store each value together with the minimum of everything below it.arrow_forward
  4. 4Evaluate Reverse Polish Notation (LeetCode 150)MediumEvaluate a postfix expression: push numbers, and each operator pops two operands and pushes the result.arrow_forward
  5. 5Daily Temperatures (LeetCode 739)MediumFor each day, count days until a warmer one: a decreasing stack of indices resolves each day when it's beaten.arrow_forward
  6. 6Next Greater Element II (LeetCode 503)MediumNext greater element in a circular array: run the monotonic stack over two laps using i % n.arrow_forward
  7. 7Online Stock Span (LeetCode 901)MediumReturn each day's stock span online: a decreasing stack of (price, span) lets a new price absorb smaller ones.arrow_forward
  8. 8Remove K Digits (LeetCode 402)MediumDelete k digits to make the smallest number: greedily pop a bigger digit whenever a smaller one follows.arrow_forward
  9. 9Asteroid Collision (LeetCode 735)MediumSimulate colliding asteroids: the stack holds survivors, and each left-mover fights right-movers on top.arrow_forward
  10. 10Largest Rectangle in Histogram (LeetCode 84)HardFind the biggest rectangle in a histogram: an increasing stack finds each bar's left and right limits in O(n).arrow_forward
  11. 11Trapping Rain Water (LeetCode 42)HardTotal the rain trapped between bars: water over a bar is set by the smaller of the tallest bars on each side.arrow_forward