Data Structures & AlgorithmsHardProduct Company SDE · binary search on answer

Problem: Split [7, 2, 5, 10, 8] into 2 contiguous parts to minimise the largest part sum. What is the answer? Run the brute-force code.

Problem: Split [7, 2, 5, 10, 8] into 2 contiguous parts to minimise the largest part sum. What is the answer? Run the brute-force code.
a = [7, 2, 5, 10, 8]
best = float('inf')
for k in range(1, len(a)):
    best = min(best, max(sum(a[:k]), sum(a[k:])))
print(best)
What is printed?
  1. A. 17
  2. B. 20
  3. C. 18
  4. D. 15

Correct answer: C. 18

Splitting into [7, 2, 5] and [10, 8] gives largest parts 14 and 18, so 18 is the minimum over all two-part splits.

More Coding Interview Problems questions

Exams that ask this topic