🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Monotonic Stack: all 13 questions

Quiz: Monotonic Stack · Question 11 of 13

What does this print? (code)

What does this print?

t = [73, 74, 75, 71, 69, 72, 76, 73]
ans = [0] * len(t)
st = []
for i, x in enumerate(t):
    while st and t[st[-1]] < x:
        j = st.pop()
        ans[j] = i - j
    st.append(i)
print(ans)
  1. A.[1, 1, 3, 2, 1, 1, 0, 0]
  2. B.[1, 1, 4, 2, 1, 1, 1, 0]
  3. C.[74, 75, 76, 72, 72, 76, 0, 0]
  4. D.[1, 1, 4, 2, 1, 1, 0, 0]
Answer & explanation

Correct answer: [1, 1, 4, 2, 1, 1, 0, 0]

Each answer is the number of days until a warmer day. 75 waits 4 days for 76; the last two days never get warmer.

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 →