Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Arrays & Strings Mastery

Core Patterns

  • Two Pointers Pattern
  • Sliding Window Pattern
  • Prefix Sums

String Problems

  • String Hashing & Anagrams
  • String Two-Pointer Problems
Chaturmind
← Arrays & Strings Mastery

Core Patterns

  • Two Pointers Pattern
  • Sliding Window Pattern
  • Prefix Sums

String Problems

  • String Hashing & Anagrams
  • String Two-Pointer Problems
HomeLearnDSAArrays & Strings MasteryCore Patterns
✓ FreeIntermediate· 14 min read

Sliding Window Pattern

Max/min subarrays and substrings — fixed and variable window techniques.

Published March 12, 2025


Sliding Window Pattern

Sliding window avoids redundant computation when processing a contiguous subarray or substring. Instead of recomputing the entire window, we incrementally add the new element and remove the old one.

When to use it

  • Find max/min/sum in a subarray of fixed size
  • Find the smallest/longest subarray/substring satisfying a condition
  • Anything involving "contiguous" elements with a constraint

Template 1: Fixed-size window

// Maximum sum subarray of size k
public int maxSumFixed(int[] nums, int k) {
    int windowSum = 0;
    for (int i = 0; i < k; i++) windowSum += nums[i]; // initial window

    int maxSum = windowSum;
    for (int i = k; i < nums.length; i++) {
        windowSum += nums[i] - nums[i - k]; // slide: add new, remove old
        maxSum = Math.max(maxSum, windowSum);
    }
    return maxSum;
}

Template 2: Variable-size window (expand right, shrink left)

// Longest substring with at most k distinct characters
public int longestSubstringKDistinct(String s, int k) {
    Map<Character, Integer> freq = new HashMap<>();
    int left = 0, maxLen = 0;

    for (int right = 0; right < s.length(); right++) {
        // Expand window
        freq.merge(s.charAt(right), 1, Integer::sum);

        // Shrink window if constraint violated
        while (freq.size() > k) {
            char c = s.charAt(left);
            freq.merge(c, -1, Integer::sum);
            if (freq.get(c) == 0) freq.remove(c);
            left++;
        }

        maxLen = Math.max(maxLen, right - left + 1);
    }
    return maxLen;
}

Classic Problems

ProblemWindow TypeKey Data Structure
Max sum subarray of size kFixedRunning sum
Longest substring without repeatingVariableHashMap (last seen index)
Minimum window substringVariableHashMap (char counts)
Fruits into basketsVariableHashMap (fruit → count)
Max consecutive ones IIIVariableCount of zeros in window

The variable window pattern in words

  1. Expand right pointer — include new element
  2. Check constraint — if violated, shrink left pointer until valid
  3. Record max/min window size

Interview Tip

The variable window pattern is the most common. The key decision: what is the constraint? Once you identify it (distinct chars ≤ k, no duplicates, sum ≤ target), the template is the same.

Previous

Two Pointers Pattern

Next

Prefix Sums

AI Tutor

Lesson: Sliding Window Pattern

Quick actions

AI responses can be inaccurate. Verify critical information.