Two Pointers & Sliding Window — Complete Guide (Problems 101–160)

Sanjeev SharmaSanjeev Sharma
7 min read

Advertisement

Series Introduction

Welcome to the Two Pointers & Sliding Window series — the second chapter of our MAANG DSA Roadmap. This section covers 60 problems (#101–#160) grouped by pattern, from easy warm-ups to Google Hard problems.

Previous Series: Arrays & Strings (Problems 1–100)


Why Two Pointers & Sliding Window?

These are among the most asked patterns at FAANG/MAANG. They convert naive O(n²) brute-force solutions into clean O(n) algorithms by maintaining a structure that avoids redundant work.

PatternCore IdeaTypical Complexity
Fixed WindowSlide a window of size kO(n)
Variable WindowShrink/expand based on conditionO(n)
Two Pointers (opposite ends)Meet in middleO(n)
Two Pointers (same direction)Fast/slow or read/writeO(n)
at-most(k) − at-most(k−1)Count "exactly k"O(n)
Prefix Sum + DequeHandle negativesO(n)

The Mental Model

Fixed Window:
  [l -------- r]  → slide right, always size k
  Sum: s += a[r] - a[r-k]

Variable Window (expand-then-shrink):
  l=0
  for r in range(n):
      add a[r] to window
      while window invalid:
          remove a[l], l++
      ans = max(ans, r-l+1)

Two PointersInward:
  l=0, r=n-1
  while l < r:
      if condition: l++
      else: r--

Two PointersSame Direction (read/write):
  write=0
  for read in range(n):
      if keep(a[read]):
          a[write++] = a[read]

Problem Index

🟢 Easy (101–110)

#ProblemPatternDifficulty
101Valid PalindromeTwo Ptr InwardEasy
102Reverse StringTwo Ptr SwapEasy
103Squares of Sorted ArrayTwo Ptr L/REasy
104Remove ElementRead/Write PtrEasy
105Remove Duplicates (Sorted)Write PtrEasy
106Merge Sorted Arrays3-Ptr from EndEasy
107Is SubsequenceTwo PtrEasy
108Max Consecutive Ones IIISliding WindowEasy
109Diet Plan PerformanceFixed WindowEasy
110Running Sum 1DPrefix SumEasy

🟡 Medium (111–150)

#ProblemPatternDifficulty
111Longest Substring Without RepeatingVariable WindowMedium
112Longest Repeating Char ReplacementWindow + FreqMedium
113Permutation in StringFixed WindowMedium
114Fruit Into BasketsK-Distinct WindowMedium
115Max Erasure ValueHashSet WindowMedium
116Minimum Size Subarray SumVariable WindowMedium
117Subarrays Size K & Avg≥ThresholdFixed WindowMedium
118Count Subarrays Max=KCounting WindowMedium
1194Sum IITwo Sum on HalvesMedium
1203SumSort + Two PtrMedium
121Two Sum II (Sorted)Two PtrMedium
122Container with Most WaterGreedy Two PtrMedium
123Boats to Save PeopleGreedy Two PtrMedium
124Partition LabelsTwo Ptr + Last OccMedium
125Subarray Product Less Than KSliding WindowMedium
126Max Points from CardsFixed Window (n-k)Medium
127Min Ops to Reduce X to ZeroLongest Mid SubarrayMedium
128Count Nice SubarraysAt-Most TrickMedium
129Binary Subarrays with SumAt-Most TrickMedium
130Subarrays K Different IntegersExactly-K WindowMedium
131Frequency of Most Frequent ElementSorted + WindowMedium
132Replace Substring Balanced StringTwo PtrMedium
133Longest Subarray of 1s Deleting OneVariable WindowMedium
134Min Flips for Alternating BinarySliding WindowMedium
135Take K of Each CharacterWindow Max MiddleMedium
136Max Vowels in Substring of Len KFixed WindowMedium
137Grumpy Bookstore OwnerFixed Window BonusMedium
138Min Swaps to Group 1s TogetherFixed WindowMedium
139Longest Mountain in ArrayTwo Ptr ExpandMedium
140Longest Turbulent SubarrayTwo Ptr/DPMedium
141Min Diff Highest-Lowest K ScoresSorted WindowMedium
142Get Equal Substrings Within BudgetSliding WindowMedium
143Subarrays with All Three CharsThree PtrMedium
144Subarray Sum Divisible by KPrefix + ModuloMedium
145Find K Closest ElementsBinary Search + Two PtrMedium
146Sort Vowels in a StringTwo PtrMedium
147Count Subarrays with Fixed BoundsSliding WindowMedium
148Max Consecutive Ones (Flip)Variable WindowMedium
149Sliding Window MedianTwo HeapsHard+
150Number of Substrings with All 3 CharsThree PtrMedium

🔴 Hard (151–160)

#ProblemPatternDifficulty
151Minimum Window Substringhave/need CounterHard
152Sliding Window MaximumMonotonic DequeHard
153Shortest Subarray Sum≥KDeque on PrefixHard
154Minimum Window SubsequenceTwo Ptr DPHard
155Count Subarrays Median=KPrefix BalanceHard
156Longest Substring Two DistinctVariable WindowHard
157Substring Concatenation All WordsMulti-WindowHard
158Min Consecutive Cards to Pick UpHashMap WindowHard
159Count Subarrays Score < KTwo PtrHard
160Two Pointers & Sliding Window RecapAll PatternsSummary

Core Template Cheatsheet

# 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

# Variable Window (shrink when invalid)
def variable_window(nums, condition):
    left = best = 0
    window_state = initial_state()
    for right, val in enumerate(nums):
        add(val, window_state)
        while not valid(window_state):
            remove(nums[left], window_state)
            left += 1
        best = max(best, right - left + 1)
    return best

# Exactly K = atMost(K) - atMost(K-1)
def exactly_k(nums, k):
    return at_most(nums, k) - at_most(nums, k-1)

# Two Pointer Inward
def two_pointer(arr):
    l, r = 0, len(arr) - 1
    while l < r:
        if condition(arr[l], arr[r]): l += 1
        else: r -= 1

Next in Series

➡️ Hashing & Maps (Problems 161–220)

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro