← Quiz: Monotonic Stack: all 13 questions
Quiz: Monotonic Stack · Question 1 of 13
What does this print? (code)
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)
- A.[5, 2, 5, -1]
- B.[5, 5, 5, -1]
- C.[1, 2, 5, -1]
- D.[-1, 3, 3, -1]
Answer & explanation
Correct answer: [5, 2, 5, -1]
3's next greater is 5, 1's is 2, 2's is 5 and 5 has none.
Can you get the rest right without peeking?
Take the timed Quiz: Monotonic Stack quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →