Data Structures & AlgorithmsVery hardTCS NQT · Infosys pattern

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

  1. A. 25
  2. B. 16
  3. C. 9
  4. D. 34

Correct answer: A. 25

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