patternlens©
01Home02Patterns03Questions04Cheatsheet05Notes06Roadmaps07Dashboard

© 2026 PatternLens

patternlens©
Explore
HomePatternsQuestionsCheatsheetNotes
patternlens©
01Home02Patterns03Questions04Cheatsheet05Notes06Roadmaps07Dashboard

© 2026 PatternLens

patternlens©
Explore
HomePatternsQuestionsCheatsheetNotes
Cheatsheet›Sliding Window
CORE PATTERNVery High Frequency

Sliding Window

Maintain a contiguous sub-segment of an array or string using two boundary pointers. The window expands by moving the right pointer and shrinks by moving the left pointer, avoiding repeated O(N) recalculations on overlapping regions.

TimeO(N)
SpaceO(1) or O(K)
DifficultyMedium

Key Insight

Grow right, shrink left — process each element at most twice.

Flip ⟲

Flip for Infographic

Interactive infographic on the flip side.

Click anywhere to Flip ⟲
FLIP SIDESliding Window
Click anywhere to flip back ⟲
Sliding Window Cheatsheet

Recognition Signals (When to use this pattern)

  • →Contiguous subarray or substring
  • →Longest or shortest sequence matching a condition
  • →Maximum or minimum sum/product of a subarray
  • →At most K distinct elements or flips
  • →Exactly K occurrences (use at-most-K trick)
  • →Contains all characters of a pattern
  • →Fixed-size block statistics (average, max, sum)
  • →Sliding range query (max/min in every window of size K)
1

Types of Problems (Subpatterns)

Each subpattern solves a specific type of problem. Click on a card to view its template.

Fixed Size Window

Window width is constant — slide by adding right element and removing left.

Common Problems

  • • Maximum Sum Subarray of Size K
  • • Maximum Average Subarray I
  • • Find All Anagrams in a String
  • • Permutation in String
  • • Grumpy Bookstore Owner
5 problems

Variable Size Window — Longest Valid

Expand greedily; shrink only when the window breaks the constraint.

Common Problems

  • • Longest Substring Without Repeating Characters
  • • Longest Substring with At Most Two Distinct Characters
  • • Longest Substring with At Most K Distinct Characters
  • • Fruit Into Baskets
  • • Max Consecutive Ones III
6 problems

Variable Size Window — Shortest Valid

Expand until window satisfies the goal; then shrink as far as possible.

Common Problems

  • • Minimum Size Subarray Sum
  • • Minimum Window Substring
  • • Minimum Operations to Reduce X to Zero
  • • Smallest Subarray with Sum >= K
4 problems

Exactly K — At Most K Trick

exact(K) = atMost(K) − atMost(K−1). Converts exact-count problems into two window calls.

Common Problems

  • • Subarrays with K Different Integers
  • • Count Number of Nice Subarrays
  • • Binary Subarrays With Sum
  • • Number of Substrings Containing All Three Characters
  • • Subarray Product Less Than K
5 problems

Window with Monotonic Deque

Track the max or min inside every window in O(1) using a monotonic deque.

Common Problems

  • • Sliding Window Maximum
  • • Sliding Window Minimum
  • • Longest Continuous Subarray With Absolute Diff <= Limit
  • • Jump Game VI
  • • Constrained Subsequence Sum
5 problems

Window on Two Strings / Matching

Slide a window over one string while checking coverage of a target pattern.

Common Problems

  • • Minimum Window Substring
  • • Permutation in String
  • • Find All Anagrams in a String
  • • Number of Substrings Containing All Three Characters
  • • Check If a String Contains All Binary Codes of Size K
5 problems
2

Template / Approach

Template for: Fixed Size Window

1

Build First Window

Compute the sum or frequency map for the first K elements (index 0 to K-1).

2

Evaluate Initial Window

Check or record the answer for the first window before sliding.

3

Slide Right

For each index R from K to N-1: add nums[R] and subtract nums[R-K] in O(1).

4

Update Answer

After each slide, evaluate the window and record max, min, or match result.

template_cpp
💡 Universal fixed-size window skeleton. Swap in your own state type and condition.
1int fixedWindow(vector<int>& nums, int k)
2{
3   // ── Step 1: build first window ──────────────────
4   int windowState = 0;
5   for (int i = 0; i < k; i++)
6   {
7      windowState += nums[i];
8   }
9
10   // ── Step 2: record answer for first window ───────
11   int best = windowState;
12
13   // ── Step 3: slide window from index k to N-1 ─────
14   for (int R = k; R < (int)nums.size(); R++)
15   {
16      windowState += nums[R]; // add incoming right element
17      windowState -= nums[R - k]; // remove outgoing left element
18
19      // ── Step 4: update answer ──────────────────────
20      best = max(best, windowState);
21   }
22
23   return best;
24}
Key Idea:Window sum/state changes by exactly one addition and one subtraction per step — O(1) update instead of O(K) recomputation. Window size never changes.

Practice Problems

Sharpen your skills with hand-picked problems.

View All Problems
Easy

Maximum Sum Subarray of Size K

LeetCode #

Easy

Maximum Average Subarray I

LeetCode #643

Medium

Grumpy Bookstore Owner

LeetCode #1052

Medium

Maximum Points You Can Obtain from Cards

LeetCode #1423

Hard

Min Num of K Consecutive Bit Flips

LeetCode #995

Medium

Permutation in String

LeetCode #567

Medium

Find All Anagrams in a String

LeetCode #438

Medium

Number of Substrings Containing All Three Characters

LeetCode #1358

Medium

Longest Substring Without Repeating Characters

LeetCode #3

Medium

Longest Substring with At Most Two Distinct Characters

LeetCode #159

Medium

Longest Substring with At Most K Distinct Characters

LeetCode #340

Medium

Fruit Into Baskets

LeetCode #904

Medium

Max Consecutive Ones III

LeetCode #1004

Medium

Longest Repeating Character Replacement

LeetCode #424

Medium

Minimum Size Subarray Sum

LeetCode #209

Hard

Minimum Window Substring

LeetCode #76

Medium

Minimum Operations to Reduce X to Zero

LeetCode #1658

Medium

Subarray Product Less Than K

LeetCode #713

Medium

Binary Subarrays With Sum

LeetCode #930

Medium

Count Number of Nice Subarrays

LeetCode #1248

Hard

Subarrays with K Different Integers

LeetCode #992

Hard

Sliding Window Maximum

LeetCode #239

Medium

Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit

LeetCode #1438

Medium

Jump Game VI

LeetCode #1696

Hard

Constrained Subsequence Sum

LeetCode #1425

patternlens.

See the patterns. Master the algorithms. Ace the interviews.

Visual DSA pattern library built to turn complex algorithms into clear, intuitive structures.

Learn

  • Patterns
  • Questions
  • Notes
  • Cheatsheet

Build

  • Roadmaps
  • Notes

© 2026 PatternLens. All rights reserved.

Designed & built with care

FAQsPrivacy PolicyCookies Policy