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?
- A. 1
- B. 4
- C. 6
- 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
- Array d = [1, 2, 1, 3, 4, 3, 5]. Count the contiguous subarrays containing exactly three distinct values. Run the brute-force code. python…
- Houses have values e = [2, 7, 9, 3, 1, 8, 4, 6] and a robber cannot take two adjacent houses. Find the maximum sum that can be taken…
- Count the subarrays of v = [4, 6, 2, 1, 7, 3, 5, 1] whose XOR equals 0, considering only subarrays of length at least 3. Brute-force all…
- Histogram bars have heights h = [2, 1, 5, 6, 2, 3]. What is the area of the largest rectangle that fits entirely inside the histogram…
- What is the maximum sum of a non-empty subarray in the circular array [8, -8, 9, -9, 10, -11, 12], where the subarray may wrap from the…
- What is the maximum product of a non-empty contiguous subarray of [2, -5, -2, -4, 3]?