Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an integer array nums, find the subarray with the largest sum, and return its sum.
Example 1:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The subarray [4,-1,2,1] has the largest sum 6.
Example 2:
Input: nums = [1] Output: 1 Explanation: The subarray [1] has the largest sum 1.
Example 3:
Input: nums = [5,4,-1,7,8] Output: 23 Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
Constraints:
1 <= nums.length <= 105-104 <= nums[i] <= 104Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
Problem summary: Given an integer array nums, find the subarray with the largest sum, and return its sum.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[-2,1,-3,4,-1,2,1,-5,4]
[1]
[5,4,-1,7,8]
best-time-to-buy-and-sell-stock)maximum-product-subarray)degree-of-an-array)longest-turbulent-subarray)maximum-score-of-spliced-array)class Solution {
public int maxSubArray(int[] nums) {
// Kadane's algorithm:
// bestEnding = best subarray ending at current index
// bestOverall = best subarray seen anywhere so far
int bestEnding = nums[0];
int bestOverall = nums[0];
for (int i = 1; i < nums.length; i++) {
bestEnding = Math.max(nums[i], bestEnding + nums[i]);
bestOverall = Math.max(bestOverall, bestEnding);
}
return bestOverall;
}
}
func maxSubArray(nums []int) int {
bestEnding := nums[0]
bestOverall := nums[0]
for i := 1; i < len(nums); i++ {
if bestEnding+nums[i] > nums[i] {
bestEnding = bestEnding + nums[i]
} else {
bestEnding = nums[i]
}
if bestEnding > bestOverall {
bestOverall = bestEnding
}
}
return bestOverall
}
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
best_ending = nums[0]
best_overall = nums[0]
for x in nums[1:]:
best_ending = max(x, best_ending + x)
best_overall = max(best_overall, best_ending)
return best_overall
impl Solution {
pub fn max_sub_array(nums: Vec<i32>) -> i32 {
let mut best_ending = nums[0];
let mut best_overall = nums[0];
for &x in nums.iter().skip(1) {
best_ending = std::cmp::max(x, best_ending + x);
best_overall = std::cmp::max(best_overall, best_ending);
}
best_overall
}
}
function maxSubArray(nums: number[]): number {
let bestEnding = nums[0];
let bestOverall = nums[0];
for (let i = 1; i < nums.length; i++) {
bestEnding = Math.max(nums[i], bestEnding + nums[i]);
bestOverall = Math.max(bestOverall, bestEnding);
}
return bestOverall;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.