Loading section...
Fixed-Width Windows: The Add-Subtract Trick
Concepts: pyFixedWindow, pyWindowSum
Fixed-width sliding windows are the simplest form. The window is always exactly K elements wide. As the window slides one position right, one element enters on the right and one element leaves on the left. The window state (sum, count, product) is updated by adding the entering element and subtracting the leaving element. No inner loop needed. One pass through the array. Maximum Sum of K Consecutive Elements This is the canonical beginner sliding window problem and the most frequently asked warm-up in data engineering phone screens. Given an array and integer K, find the maximum sum among all contiguous subarrays of length K. The brute force sums each window independently: O(n*K). The sliding window computes the first window's sum, then slides. Walk through this with arr = [2, 1, 5, 1, 3,