Software Engineer (SDE)dsaintermediate3 interactive MCQs in app
Monotonic Stack / Queue
storing indices vs raw valuesamortized O(n) single push + single pop boundpopping while new element breaks monotonicity
Core Concept
A Monotonic Stack stores **indices** maintaining strictly increasing or decreasing values from bottom to top, solving "Next Greater Element" or "Largest Histogram" in **O(n)** total time instead of O(n^2)! Every index is pushed ONCE and popped AT MOST ONCE (`while stack and temps[i] > temps[stack[-1]]:`).
Worked Example
(`temps = [73, 74, 75, 71, 69, 72]`) push `0(73)`; `1(74) > 73` pops `0 -> ans[0]=1`, push `1`; `2(75) > 74` pops `1 -> ans[1]=1`; `71, 69` wait; `72` pops `69` (`ans=1`) and `71` (`ans=2`).
Common Pitfall
pushing values instead of **indices** loses the ability to compute distances (`i - prev_idx`).
Recommended Readings & Practice
Practice Monotonic Stack / Queue with 3 graded MCQs & written feedback.
Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.