Data Structures & AlgorithmsHardTCS CodeVita · Microsoft SDE OA

Array c = [1, 2, 3, 0, 3, 3, 0, 6]. In how many ways can it be split into three non-empty contiguous parts whose sums are all equal? Run the brute-force count over the two cut positions.

Array c = [1, 2, 3, 0, 3, 3, 0, 6]. In how many ways can it be split into three non-empty contiguous parts whose sums are all equal? Run the brute-force count over the two cut positions.
c=[1,2,3,0,3,3,0,6]
n=len(c)
print(sum(1 for i in range(1,n-1) for j in range(i+1,n) if sum(c[:i])==sum(c[i:j])==sum(c[j:])))
What is printed?
  1. A. 1
  2. B. 4
  3. C. 6
  4. D. 2

Correct answer: B. 4

A split is a pair of cut points giving three non-empty parts with equal sums. Comparing only the outer parts ignores the middle, using two parts instead of three is a different problem, and restricting the second cut misses valid splits.

More Coding Interview Problems questions

Exams that ask this topic