Sliding window sum
Given an integer array nums and a window size k (1 <= k <= length of nums), return a list containing the sum of every window of k consecutive elements, from the leftmost window to the rightmost — n - k + 1 sums in total. Compute each subsequent sum from the previous one in O(1) rather than re-adding k elements. Example: nums = [1, 2, 3, 4], k = 2 gives [3, 5, 7].
Implement
window_sums(nums: list[int], k: int) → list[int]Examples
in
[[1,2,3,4],2]out[3,5,7]What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 10 min
solution.py
InputExpectedGot
[[1,2,3,4],2][3,5,7]not run yetsample