Question presented to candidate: "You know Kadane's algorithm for the maximum subarray sum. Can you prove it is correct? Then describe a divide-and-conquer algorithm for the same problem, prove that one too, and compare them. Sketch both in Python."
What a strong answer should cover:
- Kadane's loop invariants (best sum ending at i, best so far) with initialisation, maintenance and termination, and the lemma
E(i) = max(a[i], E(i-1) + a[i])proved by an exchange argument. - Divide and conquer: the left / right / crossing case split, why the crossing sum is best suffix + best prefix, and the strong-induction proof.
- Costs: T(n) = 2T(n/2) + O(n) = O(n log n) for the crossing version versus O(n) and O(1) space for Kadane.
- The four-value (total, prefix, suffix, best) combine that makes divide and conquer O(n) and becomes a segment tree for range queries.
- Python specifics:
(lo + hi) // 2is safe because ints do not overflow, recursion depth is only about log₂ n, and passing bounds instead of slicing avoids copies.
Code / implementation expected: Optional — the proofs matter most; a Python sketch of the crossing-sum recursion and Kadane's loop is a strong finish.