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

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


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! ๐Ÿš€

1
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.