Maximum Subarray Sum with One Deletion is a strategic optimization problem. You are given an array of integers and you want to find the maximum sum of a non-empty contiguous subarray. However, you are given a special "one-time" power: you can delete at most one element from your chosen subarray to make its sum larger. For example, in the array [1, -2, 0, 3], the maximum subarray sum without deletion is 3. But if you pick [1, -2, 0, 3] and delete -2, the sum becomes 4.
This "Maximum Subarray Sum with One Deletion interview question" is a staple at Microsoft, Meta, and Amazon. It tests if a candidate can handle complex Dynamic Programming transitions. It specifically evaluates whether you can track two parallel possibilities: the best sum without any deletions and the best sum where one deletion has already occurred. It's a great test of logical consistency and ability to handle "either-or" scenarios in code.
The "Array, Dynamic Programming interview pattern" is used here to maintain two arrays (or variables) as we iterate. Let noDeletion[i] be the maximum subarray sum ending at i with zero elements deleted. This is just standard Kadane’s. Let oneDeletion[i] be the maximum subarray sum ending at i where exactly one element has been deleted. To calculate oneDeletion[i], you either take noDeletion[i-1] (meaning you just deleted the current element arr[i]) or oneDeletion[i-1] + arr[i] (meaning you already deleted an element earlier).
Consider the array [5, -10, 6].
The most common mistake in the "Maximum Subarray Sum with One Deletion coding problem" is not handling the "non-empty" constraint correctly. If the array is all negative, like [-1, -1, -1], you still have to pick at least one element (e.g., -1). Another error is failing to consider that deleting an element effectively "joins" the subarray from the left and the subarray from the right. While the two-variable DP approach is better, some candidates try a "left-sum and right-sum" approach and miss edge cases.
Practice the "state-based" DP approach. Instead of thinking about the deletion as a one-time event that happens at a specific index, think of it as a state you can transition into. This makes the logic much more robust. Also, always check if your solution handles the case where no deletion is performed at all (since the problem says "at most" one deletion). Comparing your two DP states at every step is crucial for finding the true maximum.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find the Maximum Length of Valid Subsequence I | Medium | Solve | |
| Maximum Absolute Sum of Any Subarray | Medium | Solve | |
| Minimum Score Triangulation of Polygon | Medium | Solve | |
| Solving Questions With Brainpower | Medium | Solve | |
| Find the Maximum Length of Valid Subsequence II | Medium | Solve |