Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: Kadane's algorithm finds the maximum sum of a non-empty contiguous subarray of [−2, 1, −3, 4, −1, 2, 1, −5, 4]. What is that maximum?

  1. A. 4
  2. B. 1
  3. C. 6
  4. D. 12

Correct answer: C. 6

Kadane keeps the best sum ending here, max(x, cur + x); the best subarray is [4, −1, 2, 1] = 6. The largest single element is 4, the whole-array sum is 1, and adding all positive values while ignoring contiguity gives 12.

More Coding Interview Problems questions

Exams that ask this topic