My first instinct was brute force, which I'm pretty sure they clocked immediately.
Use a sliding window of fixed size k to compute the sum of the first k elements, then slide the window by adding the next element and subtracting the element leaving the window. Track the maximum sum seen. This achieves O(n) time and O(1) space.
Pro tip: Clarify that the window size is fixed at k, so we don't need to expand/shrink like in variable-size sliding window problems. Also, mention that initializing max_sum to negative infinity handles all-negative arrays correctly.
Confirm that we need the maximum sum of a contiguous subarray of exactly k elements, and that the array can contain negative numbers. Note the O(n) time requirement.
Since the window size is fixed, use a sliding window approach. Compute the sum of the first k elements as the initial window.
Iterate from index k to n-1, updating the window sum by adding the new element and subtracting the element that falls out of the window (at index i-k).
After each slide, compare the current window sum with the maximum sum found so far and update if larger.
After processing all windows, return the maximum sum. Handle edge cases like k > n by returning 0 or throwing an error, depending on requirements.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.