Back to DSA & Algorithms
DSA & Algorithms
easy
mid

How would you find the maximum sum of a subarray within a given window size?

Fixed-size sliding window: compute the first window's sum, then slide by adding the incoming element and subtracting the outgoing one. O(n) time, O(1) space — no need to recompute each window.

4 min read·~15 min to think through

This is the canonical fixed-size sliding window problem. The naive solution recomputes every window in O(n·k); the trick is that consecutive windows overlap by k−1 elements, so you only adjust by the difference.

Approach

  1. Sum the first k elements — that's the first window.
  2. Slide: for each new position, add the entering element, subtract the leaving element. That's O(1) per step.
  3. Track the max as you go.
js
function maxSubarraySum(nums, k) {
  if (nums.length < k) return null; // not enough elements

  let windowSum = 0;
  for (let i = 0; i < k; i++) windowSum += nums[i];

  let max = windowSum;
  for (let i = k; i < nums.length; i++) {
    windowSum += nums[i] - nums[i - k]; // slide
    max = Math.max(max, windowSum);
  }
  return max;
}

Why it's O(n)

Each element is added exactly once and removed exactly once. No nested loop. Space is O(1) — just the running sum and the max.

Don't confuse this with Kadane's

  • This problem: window of a fixed size k → sliding window.
  • "Maximum subarray sum" with no size constraintKadane's algorithm (current = max(num, current + num)). Different problem, different technique. Interviewers sometimes blur the wording to see if you notice.

Variants

  • Max average of a window — same thing, divide by k.
  • Variable-size window ("smallest window with sum ≥ target") → two-pointer expand/contract.
  • Negative numbers — the fixed-window version still works unchanged; just don't assume the max is positive.

Follow-up questions

  • How is this different from Kadane's algorithm?
  • How would you handle a variable-size window (smallest window with sum >= target)?
  • Does the solution change if the array has negative numbers?
  • How would you return the window's start index, not just the sum?

Common mistakes

  • Recomputing the whole window sum each step — O(n*k) instead of O(n).
  • Confusing this with Kadane's (unconstrained max subarray).
  • Off-by-one in the slide: subtracting nums[i-k] wrong index.
  • Not handling arrays shorter than k.

Performance considerations

  • O(n) time, O(1) space — optimal. The naive O(n*k) is the trap; the overlap insight is the whole point of the problem.

Edge cases

  • Array length < k → no valid window.
  • k equals the array length → one window.
  • All negative numbers — max is still well-defined.
  • k = 1 → just the max element.

Real-world examples

  • Moving averages over a time series (k-period window).
  • Rolling metrics — peak traffic over any 5-minute window.

Senior engineer discussion

Seniors state the O(n)/O(1) bound up front, explicitly distinguish fixed-window sliding from Kadane's, and generalize to the variable-size two-pointer pattern. The 'tell' is recognizing the overlap between consecutive windows as the source of the speedup.

Related questions