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

Quiz: Testing AI Code · Question 8 of 10

What does this print? (code)

What does this print?

def max_subarray(nums):
    best = cur = nums[0]
    for x in nums[1:]:
        cur = max(x, cur + x)
        best = max(best, cur)
    return best

print(max_subarray([-3, -8, -1]))
  1. A.0
  2. B.-12
  3. C.-3
  4. D.-1
Answer & explanation

Correct answer: -1

With all negative numbers, the best subarray is the single largest element.

Can you get the rest right without peeking?

Take the timed Quiz: Testing AI Code quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →