Data Structures & AlgorithmsHardAmazon/Microsoft SDE pattern

Kadane's algorithm runs on the array [6, 9, 2, 7, -4, -9, -5, 1, 3, 4] to find the maximum sum of a contiguous subarray. What sum does it return?

  1. A. 14
  2. B. 24
  3. C. 28
  4. D. 9

Correct answer: B. 24

Tracing Kadane's algorithm (reset the running sum to the current element whenever it drops to zero or below, else extend it) gives a best contiguous sum of 24, from index 0 to 3. 14 is the sum of the whole array (wrong when some prefix should be dropped), and 9 is just the single largest element, which ignores combining it with neighbours.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic