Skills101
← All practice quizzes

Quiz: Monotonic Stack

5 questions · new set on every retakeNo time limitPass: 60%+10 XP on first pass

A stack that keeps its values in order turns many "look left or right for the next bigger or smaller value" problems from O(n²) into O(n). Traced step by step, with the four standard variants and the classic problems.

Log in to take the quiz

Free account · see your score, answers and explanations, earn XP

  1. Question 1

    What does this print?

    a = [3, 1, 2, 5]
    ans, st = [-1] * 4, []
    for i, x in enumerate(a):
        while st and a[st[-1]] < x:
            ans[st.pop()] = x
        st.append(i)
    print(ans)
    
  2. Question 2

    For "next greater element", the stack (from bottom to top) is always:

  3. Question 3

    Why is the monotonic stack solution O(n) even with a while loop inside a for loop?

  4. Question 4

    Why push indexes instead of values onto the stack?

  5. Question 5

    Which change turns "next greater" into "next smaller"?

🎯 Today’s placement challenge — 10 questions, no login. Can you score 10/10?Take today’s challenge →