PATTERN 4: Two Pointers & Sliding Window
D
Qubits of DPK
April 11, 2026
Core DSA
Core Idea (lock this)
“Maintain a window or two moving indices while preserving an invariant.”
You are no longer scanning blindly.
You are adjusting boundaries intelligently.
If Pattern 3 removes recomputation,
Pattern 4 removes unnecessary scanning.
How to Recognize This Pattern (CRITICAL)
You should instantly think Two Pointers / Sliding Window when:
- The problem involves subarrays / substrings
- The data is sorted or can be sorted
- The question asks for longest / shortest / count
- You see words like:
30 MUST-DO INTERVIEW PROBLEMS
(10 Easy → 10 Medium → 10 Hard)
EASY (1–10): Pointer Control Basics
Goal: learn pointer movement without panic.
- #Valid Palindrome – LC 125
- #Two Sum II (Sorted Array) – LC 167
- #Remove Duplicates from Sorted Array – LC 26
- #Merge Sorted Array – LC 88
- #Is Subsequence – LC 392
- #Reverse String – LC 344
- #Sort Array By Parity – LC 905
- #Container With Most Water – LC 11
- #Maximum Average Subarray I – LC 643
- #Minimum Difference Between Highest and Lowest of K Scores – LC 1984
After these:
Pointer movement should feel
️ MEDIUM (11–20): Real Sliding Window Thinking
Write English logic first.
- #Longest Substring Without Repeating Characters – LC 3
- #Longest Repeating Character Replacement – LC 424
- #Minimum Size Subarray Sum – LC 209
- #Max Consecutive Ones III – LC 1004
- #Fruits Into Baskets – LC 904
- #Minimum Size Subarray in Infinite Array – LC 2401
- #Subarrays with Product Less Than K – LC 713
- #Permutation in String – LC 567
- #Maximum Erasure Value – LC 1695
- #Longest Substring with At Most K Distinct Characters – LC 340
Interviewers check:
This section now covers:
- At most K
- Exactly K
- Frequency maps
- Shrinking logic
- Fixed size window
- Variable size window
Can you explain
HARD (21–30): Invariants Under Pressure
These separate good from very good.
- #Minimum Window Substring – LC 76
- #Sliding Window Maximum – LC 239
- #Substring with Concatenation of All Words – LC 30
- #Maximum Points You Can Obtain from Cards – LC 1423
- #Minimum Number of K Consecutive Bit Flips – LC 995
- #Longest Continuous Subarray with Absolute Diff ≤ Limit – LC 1438
- #Constrained Subsequence Sum – LC 1425
- #Find K Closest Elements – LC 658
- #Count Number of Nice Subarrays – LC 1248
- #Shortest Unsorted Continuous Subarray – LC 581
These demand:
- Clear invariants
- Zero guesswork
- Calm boundary adjustment
The ONE Rule That Wins Interviews
Always be able to answer:
“Why am I moving the left pointer now?”
If you can’t explain it in English → interviewer won’t trust the code.
Common Interview Mistakes
- Moving both pointers blindly
- Forgetting to shrink window
- Mixing prefix sum and sliding window without reason
- Not stating window invariant clearly
INTERVIEW REVISION CARD — Pattern 4
1️⃣ Pattern in One Line
"Maintain a window or two moving indices that preserve an invariant, avoiding redundant scanning."
2️⃣ Recognize It When...
- Problem involves subarrays/substrings
- Looking for longest/shortest/count with a condition
- Input is sorted OR can use two ends
- Trigger words: "at most K", "continuous", "window", "longest", "shortest"
3️⃣ Don't Confuse With...
- Prefix Sum (P3) → static precomputation vs dynamic window
- Monotonic Stack (P8) → when next greater/smaller is needed
- Sliding Window vs Two Pointers → SW has a window condition; TP works from both ends
4️⃣ Approach in 3 Steps
- #Define the window invariant — what condition must the window always satisfy?
- #Expand right pointer, shrink left pointer when invariant breaks
- #Track answer at each valid window state
5️⃣ Invariant to Maintain
"At every step, the window [left, right] always satisfies the given condition."
6️⃣ Complexity to Justify
- Time: O(N) — each element enters and exits window at most once
- Space: O(1) or O(K) for frequency map
- Why: Two pointers never go backward
7️⃣ Edge Cases to Always Check
- Empty array/string
- Window larger than array
- All elements same
- K = 0 or K = N
- Negative numbers in sum problems
8️⃣ One Line to Say in Interview
"I'll use a sliding window — expand right to include elements, shrink left when the invariant breaks, giving O(N) time."