Data Structures & AlgorithmsHardIBM · Deloitte pattern

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

  1. A. 33
  2. B. 9
  3. C. 25
  4. D. 29

Correct answer: D. 29

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 29, from index 1 to 8. 25 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