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.
  1. #
    Valid Palindrome – LC 125
  2. #
    Two Sum II (Sorted Array) – LC 167
  3. #
    Remove Duplicates from Sorted Array – LC 26
  4. #
    Merge Sorted Array – LC 88
  5. #
    Is Subsequence – LC 392
  6. #
    Reverse String – LC 344
  7. #
    Sort Array By Parity – LC 905
  8. #
    Container With Most Water – LC 11
  9. #
    Maximum Average Subarray I – LC 643
  10. #
    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.
  1. #
    Longest Substring Without Repeating Characters – LC 3
  2. #
    Longest Repeating Character Replacement – LC 424
  3. #
    Minimum Size Subarray Sum – LC 209
  4. #
    Max Consecutive Ones III – LC 1004
  5. #
    Fruits Into Baskets – LC 904
  6. #
    Minimum Size Subarray in Infinite Array – LC 2401
  7. #
    Subarrays with Product Less Than K – LC 713
  8. #
    Permutation in String – LC 567
  9. #
    Maximum Erasure Value – LC 1695
  10. #
    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.
  1. #
    Minimum Window Substring – LC 76
  2. #
    Sliding Window Maximum – LC 239
  3. #
    Substring with Concatenation of All Words – LC 30
  4. #
    Maximum Points You Can Obtain from Cards – LC 1423
  5. #
    Minimum Number of K Consecutive Bit Flips – LC 995
  6. #
    Longest Continuous Subarray with Absolute Diff ≤ Limit – LC 1438
  7. #
    Constrained Subsequence Sum – LC 1425
  8. #
    Find K Closest Elements – LC 658
  9. #
    Count Number of Nice Subarrays – LC 1248
  10. #
    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

  1. #
    Define the window invariant — what condition must the window always satisfy?
  2. #
    Expand right pointer, shrink left pointer when invariant breaks
  3. #
    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."

Pattern 4 is now LOCKED