| Fixed Window | Variable Window | |
|---|---|---|
| Window Size | Constant (given as k) | Changes based on condition |
| Pointers | Move together | Move independently |
| Typical Ask | "Of size k..." | "Longest/shortest that..." |
| Example | Max sum of k elements | Longest substring without repeats |
| Mistake | Problem | Fix |
|---|---|---|
| Updating result at wrong time | Missing valid windows | Longest: after shrink. Shortest: during shrink. |
| Off-by-one in window size | Wrong answer | Window size = right - left + 1 |
| Not cleaning up hashmap | Wrong distinct count | Delete key when count reaches 0 |
| Using if instead of while | Incomplete shrinking | Always use while for contraction |
| Type | Time | Space |
|---|---|---|
| Fixed window | O(n) | O(1) or O(k) |
| Variable window | O(n) | O(1) or O(unique elements) |
| With hashmap | O(n) | O(alphabet size) |