Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: What is the maximum total of two non-overlapping subarrays of length 2 in [1, 5, 2, 8, 3]? Run the code.
Problem: What is the maximum total of two non-overlapping subarrays of length 2 in [1, 5, 2, 8, 3]? Run the code.
a = [1, 5, 2, 8, 3]
win = [sum(a[i:i + 2]) for i in range(len(a) - 1)]
best = 0
for i in range(len(win)):
for j in range(i + 2, len(win)):
best = max(best, win[i] + win[j])
print(best)What is printed?- A. 21
- B. 18
- C. 17
- D. 11
Correct answer: B. 18
Window sums are 6, 7, 10, 11; non-overlapping pairs need starts at least 2 apart, best 7 + 11 = 18. Allowing overlap gives 10 + 11 = 21, requiring a gap of 3 gives 6 + 11 = 17, and 11 is one window.
More Coding Interview Problems questions
- Problem: How many integers in [1, 1000] have digit sum equal to 5 and are divisible by 5? Run the code. python print(sum(1 for x in…
- Problem: Which algorithm computes the number of shortest paths between all pairs in an unweighted graph in O(V E)?
- Problem: What is the time complexity of Kadane's algorithm extended to 2D (maximum-sum submatrix) with an n x n matrix?
- Problem: A hash table uses chaining, and every insertion first scans the key’s chain to make sure the key is not already present. What is…
- Problem: Which problem does the 'LCA by Euler tour and RMQ' reduce to?
- Problem: Which property does a 'matroid intersection' problem violate in general, making it harder than a single matroid?