๐ง The Ultimate Guide to DSA Patterns (With Examples)

Table of contents
- ๐ 1. Sliding Window
- ๐ 2. Two Pointers
- ๐ 3. Fast and Slow Pointers
- ๐ 4. Prefix Sum / Difference Arrays
- ๐งฎ 5. Binary Search
- ๐งฑ 6. Monotonic Stack / Queue
- ๐ฒ 7. Tree & Recursion Patterns
- ๐ 8. Greedy
- ๐ 9. Backtracking
- ๐พ 10. Dynamic Programming (DP)
- ๐ 11. Recursion + Memoization
- ๐ 12. Bit Manipulation
- ๐งฎ 13. Union Find (Disjoint Set)
- ๐ 14. Topological Sort / Dependency Graphs
- ๐ 15. Graph Traversals (BFS/DFS)
- ๐ช 16. Shortest Path Algorithms
- ๐ฏ 17. Trie (Prefix Tree)
- ๐งฎ 18. Counting / Hashing / Frequency Maps
- ๐ 19. Heap / Priority Queue
- ๐ 20. Two Heaps
- ๐งฎ 21. Segment Tree / Fenwick Tree (BIT)
- ๐ง 22. Matrix Patterns
- ๐ข 23. Mathematical Patterns
- ๐บ๏ธ 24. Coordinate Compression
- โ 25. Sweep Line / Event Sorting
- ๐ง 26. Meet in the Middle
- ๐ฒ 27. Randomized Algorithms
- ๐งฎ 28. Kadaneโs Algorithm
- ๐งฎ 29. Merge Sort Inversions
- ๐๏ธ 30. Bitmasking / Subset Enumeration
- ๐งฌ 31. Palindrome Techniques
- ๐ง 32. Game Theory / Sprague-Grundy
- ๐ Final Thoughts
Whether you're preparing for coding interviews or just want to become a better problem solver, mastering patterns in Data Structures and Algorithms (DSA) can be a game-changer.
Instead of memorizing solutions to hundreds of problems, recognize the underlying patterns โ these are the repeatable techniques behind most coding problems.
Hereโs a comprehensive guide of 32 DSA patterns you must know, complete with recognition cues and classic examples.
๐ 1. Sliding Window
Used for: Subarrays/substrings of fixed or variable size
Recognize When: Find sum, average, or max/min of subarrays of size K
Examples:
Maximum Sum Subarray of Size K
Longest Substring Without Repeating Characters
Minimum Size Subarray Sum
๐ 2. Two Pointers
Used for: Sorted arrays, merging, comparisons
Recognize When: Finding pairs, triplets, or merging sorted arrays
Examples:
Two Sum (Sorted)
3Sum
Merge Sorted Arrays
๐ 3. Fast and Slow Pointers
Used for: Cycle detection, middle of linked list
Recognize When: Cycle, intersection, or middle node in linked list
Examples:
Linked List Cycle
Find Middle of Linked List
Happy Number
๐ 4. Prefix Sum / Difference Arrays
Used for: Range queries, frequency updates
Recognize When: Subarray/range queries or range modifications
Examples:
Subarray Sum Equals K
Difference Array Range Updates
๐งฎ 5. Binary Search
Used for: Searching in sorted or monotonic structures
Recognize When: Min/max/target value optimization in sorted array
Examples:
Binary Search
Find Minimum in Rotated Array
Aggressive Cows
๐งฑ 6. Monotonic Stack / Queue
Used for: Next/Previous Greater/Lesser Element problems
Recognize When: NGE, PGE, histogram, stock span, rainfall trapping
Examples:
Next Greater Element
Largest Rectangle in Histogram
Daily Temperatures
Trapping Rain Water
๐ฒ 7. Tree & Recursion Patterns
Used for: Tree problems with recursion or traversal
Includes:
DFS / BFS
LCA
Tree DP
Segment Trees / BIT
๐ 8. Greedy
Used for: Optimization by local decisions
Recognize When: Maximize or minimize globally using local optimum
Examples:
Activity Selection
Fractional Knapsack
Huffman Coding
๐ 9. Backtracking
Used for: Permutations, combinations, constraint satisfaction
Examples:
N-Queens
Sudoku Solver
Subsets / Permutations
๐พ 10. Dynamic Programming (DP)
Used for: Optimal solutions using overlapping subproblems
Variants:
1D/2D DP
Memoization / Tabulation
Bitmask DP, Digit DP, Subset DP, Tree DP
Examples:0/1 Knapsack
Longest Common Subsequence
Matrix Chain Multiplication
๐ 11. Recursion + Memoization
Top-down version of DP with caching results.
๐ 12. Bit Manipulation
Used for: Subsets, toggling, counting, power of 2
Examples:
Subsets
Power of Two
XOR Tricks
๐งฎ 13. Union Find (Disjoint Set)
Used for: Connected components, grouping, cycles
Examples:
Number of Provinces
Kruskalโs MST
Detect Cycle in Undirected Graph
๐ 14. Topological Sort / Dependency Graphs
Used for: Scheduling, course completion
Examples:
Course Schedule
Alien Dictionary
๐ 15. Graph Traversals (BFS/DFS)
Used for: Exploring nodes and paths in graphs
Examples:
Number of Islands
Flood Fill
Maze Solver
๐ช 16. Shortest Path Algorithms
0-1 BFS
Dijkstra
Bellman-Ford
Floyd-Warshall
๐ฏ 17. Trie (Prefix Tree)
Used for: Prefix queries, word dictionaries
Examples:
Replace Words
Word Search II
๐งฎ 18. Counting / Hashing / Frequency Maps
Used for: Most frequent elements, anagrams
Examples:
Group Anagrams
Top K Frequent Words
๐ 19. Heap / Priority Queue
Used for: Kth largest, stream-based data
Examples:
Kth Largest Element
Median from Data Stream
๐ 20. Two Heaps
Used for: Live median, partition balancing
Examples:
- Find Median from Data Stream
๐งฎ 21. Segment Tree / Fenwick Tree (BIT)
Used for: Range queries and updates
Examples:
Range Minimum/Maximum Query
Range Sum Query
๐ง 22. Matrix Patterns
Used for: Matrix traversal, spiral, diagonal
Examples:
Spiral Order
Rotate Matrix
Word Search
๐ข 23. Mathematical Patterns
GCD/LCM (Euclidโs Algorithm)
Modular Arithmetic
Sieve of Eratosthenes
Combinatorics (nCr, Factorials)
๐บ๏ธ 24. Coordinate Compression
Used for: Reducing large values to small indices for BITs or SegTrees.
โ 25. Sweep Line / Event Sorting
Used for: Intervals, events, segment merging
Examples:
Meeting Rooms
Skyline Problem
๐ง 26. Meet in the Middle
Used for: Splitting large input spaces
Examples:
Subset Sum
Partition Problem
๐ฒ 27. Randomized Algorithms
Examples:
Reservoir Sampling
Quickselect
๐งฎ 28. Kadaneโs Algorithm
Used for: Maximum Subarray Sum in O(n)
๐งฎ 29. Merge Sort Inversions
Used for: Counting inversions efficiently in O(n log n)
๐๏ธ 30. Bitmasking / Subset Enumeration
Used for: Subset-based DP, state compression
๐งฌ 31. Palindrome Techniques
Used for: Substring palindromes
Examples:
Expand Around Center
Manacherโs Algorithm
๐ง 32. Game Theory / Sprague-Grundy
Used for: Winning strategy problems
Examples:
NIM Game
XOR-based tricks
๐ Final Thoughts
DSA is less about brute-forcing and more about recognizing the right approach for the right problem. These patterns will help you:
Improve problem-solving speed
Solve unseen problems with confidence
Excel in coding interviews
๐ก Save this blog, revisit often, and practice pattern-based problems on LeetCode, Codeforces, or wherever you grind.
Happy coding! ๐
Subscribe to my newsletter
Read articles from Pradyumn Chaudhary directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Pradyumn Chaudhary
Pradyumn Chaudhary
A student, a learner, and a problem-solver. Documenting my progress and sharing what I learn along the way.