Two Pointers & Sliding Window — Master Cheatsheet (Problems 101–160)

Sanjeev SharmaSanjeev Sharma
4 min read

Advertisement

Two Pointers & Sliding Window — Complete! 🎉

60 problems covering every variant of the two-pointer and sliding window patterns. This cheatsheet summarizes all patterns for rapid review.


Pattern Templates

1. Fixed Window

def fixed_window(nums, k):
    s = sum(nums[:k])
    best = s
    for i in range(k, len(nums)):
        s += nums[i] - nums[i-k]
        best = max(best, s)
    return best

Use for: Max sum subarray of size k, Permutation in String, Vowels in Substring

2. Variable Window (Shrinkable)

def variable_window(nums, target):
    left = best = 0; state = initial()
    for right, val in enumerate(nums):
        update(state, val)           # expand
        while not valid(state):      # shrink
            undo(state, nums[left]); left += 1
        best = max(best, right - left + 1)
    return best

Use for: Longest Without Repeating, Min Size Subarray Sum, Fruit Baskets

3. Two Pointer — Inward

def two_ptr_inward(arr):
    l, r = 0, len(arr)-1
    while l < r:
        if arr[l] + arr[r] == target: return [l,r]
        elif arr[l] + arr[r] < target: l += 1
        else: r -= 1

Use for: Two Sum II, 3Sum, Container with Water, Valid Palindrome, Boats

4. Two Pointer — Same Direction (Write Ptr)

def write_ptr(nums):
    w = 0
    for r in range(len(nums)):
        if keep(nums[r]):
            nums[w] = nums[r]; w += 1
    return w

Use for: Remove Element, Remove Duplicates, Move Zeroes

5. Exactly K = atMost(K) − atMost(K−1)

def exactly_k(nums, k):
    return at_most(nums, k) - at_most(nums, k-1)

Use for: Count Nice Subarrays, Binary Subarrays with Sum, K Different Integers

6. Monotonic Deque

from collections import deque
def mono_deque_max(nums, k):
    dq = deque()  # indices, decreasing by value
    res = []
    for i, v in enumerate(nums):
        while dq and dq[0] < i-k+1: dq.popleft()
        while dq and nums[dq[-1]] < v: dq.pop()
        dq.append(i)
        if i >= k-1: res.append(nums[dq[0]])
    return res

Use for: Sliding Window Maximum, Shortest Subarray Sum ≥ K

7. Complement Window (n-k middle)

def complement_window(nums, k):
    # Find longest middle window to skip
    # Answer = n - max_middle_window

Use for: Max Points from Cards, Min Operations to Reduce X


Problem Quick Reference

Problem#PatternTime
Valid Palindrome101Inward Two PtrO(n)
Reverse String102Inward SwapO(n)
Squares Sorted Array103Outward Two Ptr → reverse fillO(n)
Remove Element104Write PtrO(n)
Remove Duplicates105Write PtrO(n)
Merge Sorted Array1063-Ptr from EndO(m+n)
Is Subsequence107Greedy Two PtrO(n)
Max Consecutive Ones III108Variable WindowO(n)
Longest Substring No Repeat111HashMap VariableO(n)
Longest Repeating Replacement112maxFreq WindowO(n)
Permutation in String113Fixed + Freq MapO(n)
Fruit Into Baskets114K-Distinct WindowO(n)
3Sum120Sort + Two PtrO(n²)
Container with Most Water122Greedy InwardO(n)
Boats to Save People123Greedy InwardO(n log n)
Subarray Product < K125Prod WindowO(n)
Max Points from Cards126Complement WindowO(n)
Count Nice Subarrays128atMost(k)-atMost(k-1)O(n)
K Different Integers131exactly(k)O(n)
Frequency of Max Freq134Sorted + WindowO(n log n)
Sliding Window Maximum152Mono DequeO(n)
Minimum Window Substring151have/need CounterO(n)
Shortest Subarray ≥ K153Deque + PrefixO(n)
Trapping Rain Water159Two Ptr + max trackingO(n)

MAANG Priority

Google: Sliding Window Maximum, Minimum Window Substring, Subarrays K Different, Shortest Subarray Sum≥K, Max Consecutive Ones III, Permutation in String

Amazon: Two Sum II, Container Most Water, Min Window Substring, Boats to Save People

Meta/Facebook: Valid Palindrome, Valid Palindrome II, Trapping Rain Water, 3Sum

Microsoft: Longest Repeating Replacement, Sort Colors, Merge Sorted Array


Next Series

➡️ Hashing & Maps (Problems 161–220)

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro