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?- A. 17
- B. 20
- C. 18
- 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
- Problem: How many contiguous subarrays of [4, 1, -1, 2, 3] have a sum divisible by 4? Run the prefix-count code. python from collections…
- Problem: How many distinct substrings of length 2 does 'abcabc' contain? Run the code. python s = 'abcabc' print(len({s[i:i + 2] for i in…
- Problem: What is the length of the longest valid (well-formed) parenthesis substring of ')()())'? Run the stack code. python s = ')()())'…
- Problem: What is the value of the reverse Polish expression '2 1 + 3 '? Run the stack code. python tokens = '2 1 + 3 '.split() st = [] for…
- Problem: What is the maximum matching size in the bipartite graph with left vertices {0,1,2}, right vertices {0,1,2} and edges (0,0)…
- Problem: How many integers x in [1, 100] satisfy x^2 ≡ 1 (mod 8)? Run the code. python print(sum(1 for x in range(1, 101) if (x x) % 8 ==…