Sliding Window Mastery (Fixed + Variable)
πΉ Why Sliding Window?
Sliding window is one of the most universal FAANG patterns.
- Google: loves optimizing substrings & arrays with O(n).
- Amazon: standard repetition of substring/array window.
- Meta (Facebook): focuses on variable windows with conditions.
- Apple: adds edge constraints (like k-size + max/min).
- Netflix: applies it in streaming/log analysis contexts.
πΉ Two Core Templates
1. Fixed-Size Sliding Window
Used when window length is known (like k).
int slidingWindowFixed(int[] arr, int k) {
int left = 0, sum = 0, maxSum = 0;
for (int right = 0; right < arr.length; right++) {
sum += arr[right];
if (right - left + 1 > k) {
sum -= arr[left];
left++;
}
maxSum = Math.max(maxSum, sum);
}
return maxSum;
}
2. Variable-Size Sliding Window
Used when window grows/shrinks based on condition.
int slidingWindowVariable(String s) {
int left = 0, maxLen = 0;
Map<Character, Integer> map = new HashMap<>();
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
map.put(c, map.getOrDefault(c, 0) + 1);
while (map.get(c) > 1) { // shrink if invalid
char leftChar = s.charAt(left);
map.put(leftChar, map.get(leftChar) - 1);
left++;
}
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
}
πΉ Flagship Problems
β Google / Amazon β Longest Substring Without Repeating Characters
- Input:
"abcabcbb"
β Output:3
("abc") - Uses variable window + hash map.
β Meta (Facebook) / Google β Minimum Window Substring
- Input:
S = "ADOBECODEBANC", T = "ABC"
β Output:"BANC"
- Uses two-pointer shrink + frequency map.
β Apple / Netflix β Sliding Window Maximum
- Input:
[1,3,-1,-3,5,3,6,7], k = 3
β Output:[3,3,5,5,6,7]
- Uses Deque (Monotonic Queue).
πΉ Company-Specific Insights
- Google: Might push for substring problems with twists (palindrome, k replacements).
- Amazon: Loves variations of longest substring with condition.
- Meta: Adds constraints like at most k distinct characters.
- Apple: Loves max/min inside fixed window β Deque trick.
- Netflix: Streaming logs β moving averages, anomaly detection with sliding window.
πΉ Takeaway
- If k is fixed β fixed window template.
- If condition defines window β variable window.
- Always track hash maps/sets for uniqueness, deques for max/min.
π
Coming Up β Day 2:
π Two Pointers / Fast & Slow Pointers (LinkedList cycles, sorted array problems, Google-style questions).
Top comments (0)