Sliding window practice
Implement max sum subarray of size k, longest substring without repeating characters, and minimum window substring.
- Implement max sum subarray of size k (fixed window)
- Implement longest substring without repeating characters (variable window with last-seen index)
- Implement minimum window substring (frequency dict with expand/shrink)
Three problems that cover the full range of window techniques. The first is fixed; the next two are variable and grow in bookkeeping complexity.
Three sliding window implementations
Key implementation notes
Longest no-repeat — jump, don't crawl. Storing the last-seen index lets the left pointer jump directly past the duplicate instead of stepping one character at a time. Both give the same result, but the jump version is faster in practice (though both remain O(n) — each character is visited at most twice).
Minimum window — formed counter avoids re-scanning. Checking whether the
window is currently valid by iterating over all required characters every step
would cost O(|t|) per step. The formed variable tracks how many character
requirements are currently satisfied in O(1) per character event.
Minimum window substring is the hardest of the three. The bookkeeping is
richer but the structure is identical: expand right to include a new
character, update state, then shrink left while the window is still valid.
The same loop appears in every variable-window problem.
Where to go next
Next: two-pointer on strings — a complementary technique where the two pointers start at opposite ends and move inward, rather than both advancing from left to right.
Sliding window
Understand fixed and variable sliding windows, identify the expand-right / shrink-left pattern, and recognise which problems call for it.
Two-pointer on strings
Use two pointers moving inward from both ends, understand palindrome checking as the canonical example, and contrast with sliding window.