Two Pointers and Sliding Window — The Complete FAANG Interview Pattern Guide
Advertisement
Problem Statement
The two pointer and sliding window family is the most frequently tested array and string pattern at FAANG. This guide indexes the 60 problems in this series and explains when to choose each variant.
Constraints:
- Patterns covered: opposite-end two pointers, fast-slow two pointers, fixed-size sliding window, variable-size sliding window
- Companies referenced: Meta, Google, Amazon, Apple, Netflix, Microsoft, Bloomberg, Uber
- Problem difficulty range: Easy (LC 125, 344) through Hard (LC 76, 239, 30)
Input: pattern selection problem
Output: opposite-end two pointers, fast-slow, fixed window, or variable windowInput: array of size n with O(n^2) brute force
Output: O(n) two pointer or sliding window solutionWhy This Problem Matters
Two pointers and sliding window account for roughly 15 to 20 percent of all array and string questions asked at FAANG, according to 2026 interview pattern data from Educative, DesignGurus, and LeetCode. Meta in particular rewards candidates who can convert an O(n^2) brute force into an O(n) two pointer solution within the first five minutes of a 45 minute loop. Google asks variable sliding window problems such as LC 76 Minimum Window Substring at the senior engineer level, and Amazon uses fixed window problems like LC 643 to test whether candidates can spot the constant-size invariant.
The pattern is also a gateway to harder topics. Once you internalize the inward-convergence idea behind LC 167 Two Sum II, you can extend it to 3Sum, 4Sum, Trapping Rain Water, and Container With Most Water. Once you understand the variable-window invariant for Longest Substring Without Repeating Characters, you can solve Permutation in String, Fruit Into Baskets, and Max Consecutive Ones III with the same template.
This series is structured to build that intuition deliberately. We start with palindrome checks and reverse-string warm-ups, then move through fixed and variable windows, and finish with hard problems that combine multiple pointers, prefix sums, and monotonic structures.
The Core Insight
Every two pointer and sliding window problem maintains an invariant on a contiguous range. The pattern variant is determined by how the range moves.
Opposite-end two pointers (LC 125, 11, 15, 167) start with left = 0 and right = n - 1 and walk inward based on a comparison. Fast-slow two pointers (LC 26, 27, 283) keep a write index lagging behind a read index. Fixed sliding window (LC 643, 1456) maintains a window of exactly k elements and slides one step at a time. Variable sliding window (LC 3, 76, 209, 904) expands the right edge until the invariant breaks, then shrinks the left edge until it is restored.
Choosing the right variant is mostly about reading the problem statement. If the array is sorted and you are searching for a pair sum, use opposite-end two pointers. If you are asked for the longest or shortest contiguous subarray satisfying some condition, use a variable window. If the window size is given as a parameter k, use a fixed window.
Visual Dry Run
The table below maps the 60 problems in this series to their pattern variant.
| Range | Pattern | Representative Problem | Difficulty |
|---|---|---|---|
| 01-07 | Opposite-end two pointers | Valid Palindrome (LC 125) | Easy |
| 08-16 | Variable sliding window | Max Consecutive Ones III (LC 1004) | Medium |
| 17-19 | Two pointer pair search | 4Sum II (LC 454) | Medium |
| 20-30 | Mixed two pointer and window | 3Sum (LC 15) | Medium |
| 31-45 | Variable window with constraints | Replace Substring (LC 1234) | Medium |
| 46-55 | Hard sliding window | Sliding Window Median (LC 480) | Hard |
| 56-60 | Synthesis problems | Trapping Rain Water (LC 42) | Hard |
Solution (Optimal)
def variable_sliding_window(arr, is_valid):
left = 0
best = 0
state = {}
for right, value in enumerate(arr):
state[value] = state.get(value, 0) + 1
while not is_valid(state):
state[arr[left]] -= 1
if state[arr[left]] == 0:
del state[arr[left]]
left += 1
best = max(best, right - left + 1)
return bestvar variableSlidingWindow = function(arr, isValid) {
let left = 0;
let best = 0;
const state = new Map();
for (let right = 0; right < arr.length; right++) {
state.set(arr[right], (state.get(arr[right]) || 0) + 1);
while (!isValid(state)) {
const leftVal = arr[left];
state.set(leftVal, state.get(leftVal) - 1);
if (state.get(leftVal) === 0) state.delete(leftVal);
left++;
}
best = Math.max(best, right - left + 1);
}
return best;
};Time: O(n) — each element enters and leaves the window at most once Space: O(k) — where k is the size of the state map
Common Mistakes
- Confusing opposite-end two pointers with fast-slow two pointers and using the wrong template
- Forgetting that variable sliding window requires the
whileshrink loop, not just a singleif - Not removing zero-count entries from the window state map, which breaks distinct-character checks
- Using sliding window on unsorted data when a hash map would be O(n) and clearer
- Returning
right - leftinstead ofright - left + 1for inclusive window length
Interview Tips
- State the pattern out loud before coding: "I will use a variable sliding window because we need the longest contiguous subarray that satisfies a constraint"
- Draw the pointers on the whiteboard with arrows so the interviewer can follow the invariant
- Always discuss the brute-force solution first, even when you know the optimal, so the interviewer hears your reasoning
- Confirm the input bounds: window techniques assume contiguous data, sometimes the problem allows reordering
Follow-up Questions
- When would you use a deque instead of two pointers for a sliding window? (Hint: Sliding Window Maximum LC 239)
- How do you adapt the variable window template when the constraint is "at most k" versus "exactly k"? (Hint: subtract two at-most calls)
- Can two pointers solve unsorted array pair sum? (Hint: no, hash map is required)
- How do you handle negative numbers in a sliding window sum problem? (Hint: prefix sum plus monotonic deque)
- What is the relationship between two pointers and binary search? (Hint: both prune half the search space)
Key Takeaways
- Two pointers and sliding window account for 15 to 20 percent of FAANG array and string interview questions
- Opposite-end two pointers run inward on sorted data with O(n) time and O(1) space
- Fast-slow two pointers maintain a write index behind a read index for in-place modification
- Fixed sliding window slides a constant-size range and is ideal for "average over k" problems
- Variable sliding window expands right until the invariant breaks, then shrinks left to restore it
- The variable window template applies to LC 3, 76, 209, 904, 1004, 1234, and roughly 30 other LeetCode problems
- Choosing the right variant is the single most important step; once chosen, the code is mechanical
Advertisement