Struggling with DSA questions? Many of them follow common patterns. Mastering these helps you quickly recognize and apply the right strategy during interviews or contests. Here's a comprehensive list with explanations, variations, keywords, and popular problems.

✅ Pattern 1: Sliding Window

🔍 Recognize it when:

  • Problem talks about subarrays/substrings
  • Mentions fixed or variable "window size"
  • Involves max/min/sum/average of a continuous sequence

📦 Data Structures:

  • Arrays, Strings, HashMaps/Sets

⏱ When to Use:

  • Optimize O(n²) brute force over continuous segments

🔁 Variations:

  • Fixed-size window (e.g. max sum of size k)
  • Dynamic window size (e.g. longest substring without repeats)
  • Hashing inside window
  • Monotonic deque for sliding max

🧩 Keywords: “subarray”, “contiguous”, “longest”, “window”

🌟 Popular Problems:

Problem Variation Approach
Maximum Sum Subarray of Size K Fixed Window Running sum
Longest Substring Without Repeating Characters Dynamic HashMap/Set
Minimum Window Substring Dynamic + HashMap Shrink/expand
Sliding Window Maximum Monotonic Deque Deque tracks max

✅ Pattern 2: Two Pointers

🔍 Recognize it when:

  • Involves sorted arrays, linked lists, or pair-based problems
  • Looking for elements that meet a condition (sum, duplicates, etc.)

📦 Data Structures: Arrays, Strings, Linked Lists

⏱ When to Use: Reduce nested loops to O(n)

🔁 Variations:

  • Start-End pointers (e.g., Two Sum)
  • Fast-Slow (e.g., remove duplicates)
  • Triple pointers (e.g., 3Sum)

🧩 Keywords: “sorted”, “pair with sum”, “in-place”, “remove duplicates”

🌟 Popular Problems:

Problem Variation Approach
Two Sum (Sorted) Start-End Move pointers
3Sum Triple Fix one, 2 pointers
Remove Duplicates Fast-Slow Overwrite duplicates
Container With Most Water Max Area Greedy inward move

✅ Pattern 3: Fast & Slow Pointers (Floyd’s Cycle)

🔍 Recognize it when:

  • Linked list or sequence may loop or cycle

📦 Data Structures: Linked Lists, Arrays

⏱ When to Use: Detect cycles or repeated states

🔁 Variations:

  • Cycle detection/start point
  • Middle of linked list

🧩 Keywords: “cycle”, “loop”, “circular”, “middle node”

🌟 Popular Problems:

Problem Variation Approach
Linked List Cycle Cycle Fast = 2x speed
Happy Number Cycle in Digits Use fast-slow
Middle of Linked List Find Middle Fast vs Slow pointer

✅ Pattern 4: Merge Intervals

🔍 Recognize it when:

  • You get a list of intervals or ranges

📦 Data Structures: Arrays of Pairs, Sorting

⏱ When to Use: Scheduling, booking overlaps, time ranges

🔁 Variations:

  • Merge overlapping
  • Insert and merge
  • Interval intersections

🧩 Keywords: “merge”, “overlap”, “intervals”, “meeting rooms”

🌟 Popular Problems:

Problem Variation Approach
Merge Intervals Merge Sort + Compare
Insert Interval Insert & Merge Binary Search
Meeting Rooms Conflict Sort + Check
Interval Intersection Overlap Two Pointers

✅ Pattern 5: Cyclic Sort

🔍 Recognize it when:

  • Array has elements from 1 to N or 0 to N
  • Asked to find missing/duplicate elements without extra space

📦 Data Structures: Arrays

⏱ When to Use: When range is known and space is tight

🔁 Variations:

  • One missing
  • One duplicate
  • Both missing & duplicate

🧩 Keywords: “1 to N”, “find missing”, “no extra space”, “in-place”

🌟 Popular Problems:

Problem Variation Approach
Cyclic Sort Basic Swap to index
Find Missing Number 0 to N Swap then scan
Find All Duplicates Detect Duplicates Swap and track
Set Mismatch Duplicate + Missing XOR or Cyclic Sort

✅ Pattern 6: Binary Search

🔍 Recognize it when:

  • You need to find a position or value in a sorted array or a range

📦 Data Structures: Arrays, Continuous Ranges

⏱ When to Use: Find values that satisfy a condition efficiently

🔁 Variations:

  • Lower Bound / Upper Bound
  • First/Last Occurrence
  • Binary Search on Answer

🧩 Keywords: “sorted”, “optimize”, “threshold”, “position”

🌟 Popular Problems:

Problem Variation Approach
Binary Search Standard Midpoint compare
First/Last Position Boundaries Adjust mid logic
Find Minimum in Rotated Array Optimized Range Track inflection
Aggressive Cows Binary Search on Answer Check placement feasibility
Find Peak Element Binary Search Mid neighbor comparison

✅ Pattern 7: Special Array Sorting

🔍 Recognize it when:

  • Array has known structure (0s/1s/2s, wave, bitonic, etc.)

📦 Data Structures: Arrays, Sometimes HashMap/Heap

⏱ When to Use: Structure allows optimized sort or O(n) trick

🔁 Variations:

  • Dutch National Flag (0s,1s,2s)
  • Cycle Sort (1 to N)
  • Counting Sort
  • Wiggle/Wave Sort
  • Min-Heap for k-sorted arrays

🌟 Popular Problems:

Problem Pattern Approach
Sort Colors Dutch Flag 3-way partition
Sort k-Sorted Array Heap Sort Min-Heap
Wiggle Sort Wiggle Sort Compare and swap
Sort Bitonic Array Merge Split + Merge

✅ Pattern 8: Hash Maps in Arrays/Linked Lists

📦 Data Structures: Arrays, Linked Lists, HashMap/Set

🔁 Use Cases:

  • Frequency Counting
  • Prefix Sums
  • Constant Time Lookups
  • Tracking visited nodes (cycle)
  • Node to Node Mapping (cloning)

🧩 Keywords: “exists”, “count”, “first/last”, “subarray sum”, “O(1) lookup”

🌟 Popular Problems:

Problem Pattern Use
Two Sum Lookup Map of values to index
Subarray Sum = K Prefix Sum Count subarrays
Group Anagrams Grouping Sorted word → list
LRU Cache Design Track key-value usage

✅ Pattern 9: Prefix & Suffix Sum

🔍 Recognize it when:

  • Asked for sum between two indices
  • Need to partition based on sums
  • Query ranges repeatedly

📦 Data Structures: Arrays, HashMaps

🔁 Variations:

  • Prefix Array: prefix[i] = sum(arr[0..i])
  • Suffix Array
  • Prefix Sum + HashMap
  • 2D Prefix Sum (for matrices)

🌟 Popular Problems:

Problem Variation Notes
Subarray Sum = K Prefix + Map Count subarrays
Pivot Index Balance Prefix = Suffix
Max Score from Cards Prefix + Suffix Try all combos

✅ Pattern 11: Monotonic Stack

🔍 Recognize it when:

  • “Next greater”, “Previous smaller”, “Stock span” type problems

📦 Data Structures: Stack (often indices), Arrays

🔁 Variations:

  • Next Greater Element
  • Previous Smaller
  • Histogram Area
  • Circular Arrays
  • Monotonic Queue

🌟 Popular Problems:

Problem Stack Type Notes
Next Greater Element Decreasing Push indices
Daily Temperatures Decreasing Count days
Largest Rectangle Increasing Area using span
Trapping Rain Water Stack Track boundaries
Stock Span Stack Track previous greater

✅ Pattern 14: Kadane’s Algorithm

🔍 Recognize it when:

  • “Maximum subarray sum”, “wrap around”, or “one deletion allowed”

📦 Data Structures: Arrays

🔁 Variations:

  • Classic Kadane
  • Circular Kadane
  • With One Deletion
  • Max Product Subarray

🌟 Popular Problems:

Problem Variation Notes
Max Subarray (Leetcode 53) Classic Track current & max
Max Sum Circular Subarray Circular max(normal, total - min)
Max Product Subarray Product Track Need min/max
Buy & Sell Stock Kadane on Profit Max diff logic

Thanks for reading till the end! 🙌🚀

See ya! 👋😄

Luffy see ya