Code of the Day
AdvancedString Algorithms

Sliding window practice

Implement max sum subarray of size k, longest substring without repeating characters, and minimum window substring.

CS FundamentalsAdvanced12 min read
Recommended first
By the end of this lesson you will be able to:
  • 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

Python — editable, runs in your browser

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.

Finished reading? Mark it complete to track your progress.

On this page