1. Introduction to the Sliding Window Pattern
The Sliding Window technique is used to transform nested O(N²) brute-force subarray/substring checks into optimal O(N) linear-time algorithms.
+─────────────────────────────────────────────────────────────────────────────+
| SLIDING WINDOW MECHANICS |
+─────────────────────────────────────────────────────────────────────────────+
Array: [ 2, 1, 5, 1, 3, 2 ], Window Size K = 3
Step 1: [ 2, 1, 5 ] 1, 3, 2 --> Sum = 8
Step 2: 2, [ 1, 5, 1 ] 3, 2 --> Sum = 8 - 2 + 1 = 7 (O(1) update!)
Step 3: 2, 1, [ 5, 1, 3 ] 2 --> Sum = 7 - 1 + 3 = 9 (Max = 9!)
Step 4: 2, 1, 5, [ 1, 3, 2 ] --> Sum = 9 - 5 + 2 = 6
2. Core Interview Questions & Solutions
Q1. Maximum Sum Subarray of Size K (Fixed Window)
Problem Statement: Given an array of integers nums and an integer k, find the maximum sum of any contiguous subarray of size k.
cpp#include <vector> #include <numeric> #include <algorithm> int maxSubArrayLenK(const std::vector<int>& nums, int k) { if (nums.size() < k) return 0; int windowSum = 0; for (int i = 0; i < k; i++) windowSum += nums[i]; int maxSum = windowSum; for (int i = k; i < nums.size(); i++) { windowSum += nums[i] - nums[i - k]; // Slide window in O(1) maxSum = std::max(maxSum, windowSum); } return maxSum; }
- Time Complexity: O(N) | Space Complexity: O(1)
Q2. Longest Substring Without Repeating Characters (Dynamic Window)
Problem Statement: Given a string s, find the length of the longest substring without duplicate characters.
pythondef lengthOfLongestSubstring(s: str) -> int: char_index_map = {} left = 0 max_len = 0 for right, char in enumerate(s): if char in char_index_map and char_index_map[char] >= left: left = char_index_map[char] + 1 # Jump left pointer char_index_map[char] = right max_len = max(max_len, right - left + 1) return max_len
- Time Complexity: O(N) | Space Complexity: O(min(N, Sigma))
Q3. Minimum Window Substring (Advanced Dynamic Window)
Problem Statement: Given two strings s and t, return the minimum window substring of s such that every character in t (including duplicates) is included in the window.
javaimport java.util.*; class Solution { public String minWindow(String s, String t) { if (s.length() < t.length()) return ""; Map<Character, Integer> targetCounts = new HashMap<>(); for (char c : t.toCharArray()) { targetCounts.put(c, targetCounts.getOrDefault(c, 0) + 1); } int required = targetCounts.size(); int formed = 0; Map<Character, Integer> windowCounts = new HashMap<>(); int left = 0, right = 0; int[] ans = {-1, 0, 0}; // length, left, right while (right < s.length()) { char c = s.charAt(right); windowCounts.put(c, windowCounts.getOrDefault(c, 0) + 1); if (targetCounts.containsKey(c) && windowCounts.get(c).intValue() == targetCounts.get(c).intValue()) { formed++; } // Try contracting window from left while (left <= right && formed == required) { c = s.charAt(left); if (ans[0] == -1 || (right - left + 1) < ans[0]) { ans[0] = right - left + 1; ans[1] = left; ans[2] = right; } windowCounts.put(c, windowCounts.get(c) - 1); if (targetCounts.containsKey(c) && windowCounts.get(c) < targetCounts.get(c)) { formed--; } left++; } right++; } return ans[0] == -1 ? "" : s.substring(ans[1], ans[2] + 1); } }
- Time Complexity: O(S + T) | Space Complexity: O(S + T)
3. Sliding Window Pattern Summary
| Problem | Window Type | Condition to Shrink Window |
|---|---|---|
| Max Subarray Size K | Fixed (K) | right >= K (slide left pointer by 1) |
| Longest Substring No Duplicates | Dynamic | When duplicate character is detected |
| Minimum Window Substring | Dynamic | While all target characters are matched |
Practice all sliding window problems on DSAMaster's practice platform.
