DEV Community

Dev Cookies
Dev Cookies

Posted on

πŸ“Œ FAANG Coding Patterns Series – Day 1

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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)