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

Quiz: Kadane's Algorithm · Question 11 of 13

What does this print? (code)

What does this print?

prices = [9, 4, 6, 2, 5, 3]
low, best = prices[0], 0
for p in prices[1:]:
    best = max(best, p - low)
    low = min(low, p)
print(best)
  1. A.2
  2. B.5
  3. C.7
  4. D.3
Answer & explanation

Correct answer: 3

Buy at 2, sell at 5 for a profit of 3. Selling at 9 is impossible because it comes before any lower price.

Can you get the rest right without peeking?

Take the timed Quiz: Kadane's Algorithm quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →