Skip to content
Code ReadingMonotonic Stack
Predict the Output

Monotonic Stack

Each new number pops the smaller ones off the stack. What is printed at the end?
1stack = []
2for x in [3, 1, 4, 1, 5]:
3 while stack and stack[-1] < x:
4 stack.pop()
5 stack.append(x)
6print(stack)