Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: What is the maximum sum of a subarray with at most one element deleted from [1, -2, 3, -4, 5]? Run the code.
Problem: What is the maximum sum of a subarray with at most one element deleted from [1, -2, 3, -4, 5]? Run the code.
a = [1, -2, 3, -4, 5]
best = -10 ** 9
for skip in range(len(a) + 1):
b = a[:skip] + a[skip + 1:] if skip < len(a) else a
for i in range(len(b)):
for j in range(i + 1, len(b) + 1):
best = max(best, sum(b[i:j]))
print(best)What is printed?- A. 5
- B. 8
- C. 9
- D. 3
Correct answer: B. 8
Deleting −4 gives [1, −2, 3, 5], whose best subarray 3 + 5 = 8. Kadane without a deletion gives 5, summing all positives (two deletions) gives 9, and the whole array sums to 3.
More Coding Interview Problems questions
- Problem: How many three-letter words over {a, b, c} contain every letter at least once? Run the code. python from itertools import product…
- Problem: What is the length of the longest subsequence of [3, 1, 4, 1, 5, 9, 2, 6] whose elements are strictly increasing? Run the code…
- Problem: What is the number of trailing zeros in 30!? Run the code. python n = 30 c = 0 p = 5 while p <= n: c += n // p p = 5 print(c)…
- Problem: How many integers in [1, 500] are perfect squares or multiples of 7? Run the code. python print(sum(1 for x in range(1, 501) if…
- Problem: What is the minimum number of operations to make all elements of [4, 7, 1] equal to their minimum by decrementing? Run the code…
- Problem: What is the number of paths from (1,1) to (3,3) moving only right or up in a grid with all cells free? Run the DP. python g =…