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 a circular integer array nums of length n, return the maximum possible sum of a non-empty subarray of nums.
A circular array means the end of the array connects to the beginning of the array. Formally, the next element of nums[i] is nums[(i + 1) % n] and the previous element of nums[i] is nums[(i - 1 + n) % n].
A subarray may only include each element of the fixed buffer nums at most once. Formally, for a subarray nums[i], nums[i + 1], ..., nums[j], there does not exist i <= k1, k2 <= j with k1 % n == k2 % n.
Example 1:
Input: nums = [1,-2,3,-2] Output: 3 Explanation: Subarray [3] has maximum sum 3.
Example 2:
Input: nums = [5,-3,5] Output: 10 Explanation: Subarray [5,5] has maximum sum 5 + 5 = 10.
Example 3:
Input: nums = [-3,-2,-3] Output: -2 Explanation: Subarray [-2] has maximum sum -2.
Constraints:
n == nums.length1 <= n <= 3 * 104-3 * 104 <= nums[i] <= 3 * 104Problem summary: Given a circular integer array nums of length n, return the maximum possible sum of a non-empty subarray of nums. A circular array means the end of the array connects to the beginning of the array. Formally, the next element of nums[i] is nums[(i + 1) % n] and the previous element of nums[i] is nums[(i - 1 + n) % n]. A subarray may only include each element of the fixed buffer nums at most once. Formally, for a subarray nums[i], nums[i + 1], ..., nums[j], there does not exist i <= k1, k2 <= j with k1 % n == k2 % n.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Monotonic Queue
[1,-2,3,-2]
[5,-3,5]
[-3,-2,-3]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #918: Maximum Sum Circular Subarray
class Solution {
public int maxSubarraySumCircular(int[] nums) {
final int inf = 1 << 30;
int pmi = 0, pmx = -inf;
int ans = -inf, s = 0, smi = inf;
for (int x : nums) {
s += x;
ans = Math.max(ans, s - pmi);
smi = Math.min(smi, s - pmx);
pmi = Math.min(pmi, s);
pmx = Math.max(pmx, s);
}
return Math.max(ans, s - smi);
}
}
// Accepted solution for LeetCode #918: Maximum Sum Circular Subarray
func maxSubarraySumCircular(nums []int) int {
const inf = 1 << 30
pmi, pmx := 0, -inf
ans, s, smi := -inf, 0, inf
for _, x := range nums {
s += x
ans = max(ans, s-pmi)
smi = min(smi, s-pmx)
pmi = min(pmi, s)
pmx = max(pmx, s)
}
return max(ans, s-smi)
}
# Accepted solution for LeetCode #918: Maximum Sum Circular Subarray
class Solution:
def maxSubarraySumCircular(self, nums: List[int]) -> int:
pmi, pmx = 0, -inf
ans, s, smi = -inf, 0, inf
for x in nums:
s += x
ans = max(ans, s - pmi)
smi = min(smi, s - pmx)
pmi = min(pmi, s)
pmx = max(pmx, s)
return max(ans, s - smi)
// Accepted solution for LeetCode #918: Maximum Sum Circular Subarray
impl Solution {
pub fn max_subarray_sum_circular(nums: Vec<i32>) -> i32 {
let (mut global_max, mut global_min) = (nums[0], nums[0]);
let (mut current_max, mut current_min) = (0, 0);
let mut total = 0;
for num in nums {
current_max = i32::max(num, current_max + num);
current_min = i32::min(num, current_min + num);
total += num;
global_max = i32::max(global_max, current_max);
global_min = i32::min(global_min, current_min);
}
if global_max > 0 {
return i32::max(global_max, total - global_min);
} else {
return global_max;
}
}
}
// Accepted solution for LeetCode #918: Maximum Sum Circular Subarray
function maxSubarraySumCircular(nums: number[]): number {
let [pmi, pmx] = [0, -Infinity];
let [ans, s, smi] = [-Infinity, 0, Infinity];
for (const x of nums) {
s += x;
ans = Math.max(ans, s - pmi);
smi = Math.min(smi, s - pmx);
pmi = Math.min(pmi, s);
pmx = Math.max(pmx, s);
}
return Math.max(ans, s - smi);
}
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.