SERIES
Sliding Window
Teach the sliding-window pattern — maintaining a window of elements and expanding/shrinking it to satisfy a constraint.
In this seriesNEWSLETTER
01
Best Time to Buy and Sell Stock: tracking the running minimum
You want to buy low and sell high, but you can only sell after you buy. The O(n) solution tracks the lowest price seen so far and computes profit at each step — it's a single pass that the sliding-window framing makes obvious.Jun 13, 2026 · 10 min read · #00017
02
Longest Repeating Character Replacement: the window validity trick
The insight is a single condition: if the window length minus the count of the most frequent character exceeds k, the window is invalid. Maintaining that condition while expanding right and shrinking left gives you the longest valid window in O(n).Jun 13, 2026 · 11 min read · #00018
03
Permutation in String: fixed-size window with a character count match
A permutation is just an anagram. So the question reduces to: does any window of length len(s1) in s2 have the same character frequencies as s1? A fixed-size sliding window with two frequency maps — or a single diff counter — answers it in O(n).Jun 13, 2026 · 15 min read · #00019
04
Minimum Window Substring: variable window with a coverage counter
The hard part isn't the window mechanics — it's knowing when the window is valid. A coverage counter that tracks how many characters in t are fully satisfied lets you shrink from the left the moment you have a valid window, without rechecking the whole map each time.Jun 13, 2026 · 13 min read · #00020
Occasional notes on what I'm building
Get an email when I publish a new post — engineering write-ups, no spam. Unsubscribe anytime.