This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 1: Arrays and strings
Grow the right edge, shrink the left, never restart.
Builds on Hash Maps & Sets, Two Pointers.
The question to ask
Is the answer a contiguous range, and can I tell from the window's running state whether it's still valid?
Questions about the best contiguous subarray or substring have an O(n²) brute force: try every start, extend every end. A window keeps the state of the current range (a count, a sum, a map of letters) and updates it incrementally as the right edge grows and the left edge shrinks. Each index enters once and leaves once, so it's O(n). Fixed-size windows slide both edges together; variable windows grow until the condition breaks, then shrink until it holds again.
Want to watch it run first? Step through the interactive walkthrough.
Time O(n) · Space O(k) for the window state
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
Add the element entering on the right, subtract the one leaving on the left. Start recording once the window reaches k.
function findMaxAverage(nums: number[], k: number): number {
let sum = 0;
let best = Number.NEGATIVE_INFINITY;
for (let right = 0; right < nums.length; right++) {
sum += nums[right];
if (right >= k) sum -= nums[right - k];
if (right >= k - 1) best = Math.max(best, sum);
}
return best / k;
}For a minimum, flip the inner loop: while the window satisfies the condition, record it, then shrink. Works here because all numbers are positive, so shrinking always lowers the sum.
function minSubArrayLen(target: number, nums: number[]): number {
let left = 0;
let sum = 0;
let best = Number.POSITIVE_INFINITY;
for (let right = 0; right < nums.length; right++) {
sum += nums[right];
while (sum >= target) {
best = Math.min(best, right - left + 1);
sum -= nums[left];
left++;
}
}
return best === Number.POSITIVE_INFINITY ? 0 : best;
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
Given daily prices, choose one day to buy and a later day to sell to maximise profit. Return the best profit, or 0.
Buy before sell: a window whose left edge is the cheapest price so far, while the right edge walks forward.
Not attempted yet
Find the contiguous subarray of size k with the maximum average value and return that average.
"Contiguous subarray of size k" is the fixed window: add the new right, subtract the old left.
Not attempted yet
Find the length of the longest substring without repeating characters.
"Longest substring" plus a condition on its contents is the variable window. Shrink whenever a character repeats.
Not attempted yet
You may replace at most k characters in the string. Return the length of the longest substring containing a single repeated letter after the replacements.
"At most k" replacements: the window is valid while its length minus its most frequent letter's count is at most k.
Not attempted yet
Return true if some permutation of s1 appears as a substring of s2.
A permutation of s1 is any window of length s1 with the same letter counts. Fixed window, compare 26 counts.
Not attempted yet
Given positive integers and a target, return the minimal length of a contiguous subarray whose sum is greater than or equal to target, or 0.
Positive numbers plus "minimal length" of a contiguous subarray: grow until the sum reaches target, then shrink while it still does.
Not attempted yet
Walking along a row of fruit trees, you can carry at most two types of fruit. Pick a contiguous run of trees and return the most fruit you can collect.
Under the story it's "longest subarray with at most k distinct values", with k = 2. A window and a count map.
Not attempted yet
Given a binary array and k, return the maximum number of consecutive 1s if you can flip at most k zeros.
"Consecutive" plus "at most k" flips: the window may contain at most k zeros.
Not attempted yet
Return the minimum window substring of s that contains every character of t, duplicates included, or an empty string.
"Minimum window" that covers a multiset: grow until every needed count is met, then shrink while it still is, tracking how many letters are satisfied.
Not attempted yet
A window of size k slides from left to right across the array. Return the maximum of each window position.
A fixed window, but max can't be un-added. A deque of indices with decreasing values gives each window's max in O(1).
Not attempted yet