DSA Patterns
DSA Patterns
2 Two Pointers Used for problems Use two pointers Two Sum (Sorted
involving sorted moving Array)
arrays, linked lists, towards/away
or string from each other. Trapping Rainwater
manipulation.
3 Fast and Slow Used for cyclic Use two pointers Linked List Cycle
Pointers problems like moving at Detection
finding loops in different speeds.
linked lists. Middle of a Linked
List
4 Merge Intervals Used for problems Sort intervals and Merge Intervals
involving merge based on
overlapping conditions. Insert Interval
intervals.
5 Cyclic Sort Used for problems Place each number Missing Number
involving numbers at its correct
in a given range (1 index. Find All Duplicate
to n). Numbers
11 Topological Sort Used for problems Use BFS or DFS to Course Schedule
with dependencies order tasks based
in a Directed on prerequisites. Alien Dictionary
Acyclic Graph
(DAG).
16 Matrix Traversal Used for problems Use BFS, DFS, or Unique Paths
involving grid dynamic
traversal. programming. Rotting Oranges
17 Heap / Priority Used for problems Use heaps for Kth Largest
Queue requiring frequent efficient insertion Element
max/min and extraction.
operations. Merge K Sorted
Lists
23 Segment Trees Used for range Build a tree Range Sum Query
query problems. structure for
efficient range Range Minimum
queries and Query
updates.
27 String Matching Used for finding Use efficient string Substring Search
(KMP, substrings in a matching
Rabin-Karp) string. algorithms. Shortest
Palindrome
28 Binary Indexed Used for dynamic Use a tree Range Sum Query
Tree (Fenwick range structure to
Tree) sum/updates. efficiently Count of Smaller
compute prefix Numbers After Self
sums.