Fixed Window
A fixed window keeps exactly k contiguous elements in scope and updates its state by adding the entering element and removing the leaving element.
What Fixed Means
A fixed window problem gives you a constant size k and asks for something over every contiguous block of that size. The block may be a subarray of numbers or a substring of characters, but its length does not change while the algorithm runs. That single fact removes a lot of complexity: once the window has length k, every move right must be paired with one move left.
This is the cleanest version of sliding window because validity is automatic. You do not ask whether the window is too long, too short, or constraint-satisfying after each step. You only maintain the aggregate for the current block and compare it with the best answer seen so far.
The Slide Operation
The slide is an exchange. When right moves to include a new element, the element at right - k is no longer inside the newest length-k window. Add the entering value first, then remove the leaving value once the window would become too large. The active window becomes the last k elements ending at right.
This is why fixed-window code often has one loop over right and a condition like right >= k for removal. The left boundary can be stored explicitly, but for a pure fixed-size window, right - k + 1 already tells you where the window begins once it is full.
Why Updates Are O(1)
A fixed window is powerful because the transition from one block to the next changes only two elements. A sum drops the leaving value and adds the entering value. A frequency array decrements one character and increments another. A count of vowels changes by checking only those two positions.
The mistake to avoid is recomputing the full block every time. Recomputing a length-k sum for each start index costs O(nk). Maintaining the running state costs O(n) because each element enters once and leaves once.
Interview Signals
Look for phrases like size k, length k, every substring of length k, maximum average over k elements, or contains nearby duplicate within k indices. These signals usually mean the window size is fixed and the main design question is which aggregate to maintain.
Fixed windows also appear inside harder problems. Permutation in String uses a fixed-length character window equal to the pattern length. Sliding Window Maximum uses a fixed-size window but needs a monotonic deque instead of a simple sum. The size is fixed, while the maintained data structure changes with the query.
Add entering, remove leaving
The window sum changes in constant time. After index right is processed, the current full window is exactly the k elements ending at right.
Key Takeaways
- Fixed-window problems keep the length exactly **k** after the first full window is formed.
- Each slide adds the entering element and removes the element that fell off the left side.
- The maintained state should update in **O(1)** whenever possible.
- The window size is fixed, but the aggregate may be a sum, count, frequency table, deque, or set.