DSA Patterns Mastery Sheet
Array & String Patterns
Two Pointers
Use two pointers moving towards each other or in the same direction to solve problems in O(n) time on sorted/linear data.
Sliding Window
Maintain a window of elements that expands/contracts to find optimal subarrays/substrings. Fixed-size or variable-size windows.
Prefix Sum
Precompute cumulative sums so that any subarray sum can be computed in O(1). Often combined with hashing.
Hashing / HashMap
Use hash tables for O(1) average lookups. Map values to indices, frequencies, or complements.
Matrix Traversal / Spiral
Traverse 2D matrices in specific orders (spiral, diagonal, layer-by-layer) or modify in-place.
Cyclic Sort
When given numbers in range [1, n], place each number at its correct index. Finds missing/duplicate numbers in O(n) time, O(1) space.
Binary Search Patterns
Binary Search
Divide search space in half each iteration. Applies to sorted arrays, rotated arrays, and answer-space searching (binary search on answer).
Linked List Patterns
Fast & Slow Pointers (Floyd's Cycle)
Use two pointers moving at different speeds to detect cycles, find midpoints, or detect patterns.
Linked List – In-Place Reversal & Manipulation
Reverse nodes in-place by manipulating pointers. Includes merge, reorder, rotation, and k-group operations.
Stack & Queue Patterns
Stack & Queue Fundamentals
LIFO/FIFO structures for parsing expressions, matching brackets, and simulating operations.
Monotonic Stack
Maintain a stack where elements are in increasing or decreasing order. Finds next greater/smaller elements efficiently.
Interval Patterns
Merge Intervals
Sort intervals by start time and merge/process overlapping intervals. Core pattern for scheduling problems.
Heap Patterns
Heap / Top K Elements
Use min-heap or max-heap to efficiently maintain the K largest/smallest/most-frequent elements.
Two Heaps / Median Pattern
Tree Patterns
Tree – BFS / Level Order Traversal
Use a queue to process tree nodes level by level. Useful for level-based operations.
Tree – DFS (Preorder/Inorder/Postorder)
Recursive or iterative depth-first traversal. Solve problems using top-down or bottom-up recursion.
BST (Binary Search Tree) Properties
Graph Patterns
Graph – BFS / DFS Traversal
Explore graphs using BFS (shortest path in unweighted) or DFS (connectivity, components). Includes grid-based graph problems.
Graph – Shortest Path (Dijkstra / Bellman-Ford)
Topological Sort
Trie Pattern
Backtracking Patterns
Backtracking – Subsets / Combinations / Permutations
Systematically explore all possible configurations by building candidates incrementally and abandoning (backtracking) when constraints are violated.
Greedy Pattern
Greedy Algorithms
Dynamic Programming Patterns
DP – Fibonacci / Linear 1D DP
Each state depends on a fixed number of previous states. The simplest DP pattern.
DP – 0/1 Knapsack
DP – Unbounded Knapsack
DP – LCS / LIS (Subsequence Patterns)
Find longest common/increasing subsequences. Core pattern: compare characters and build 2D DP table.
DP – Palindromic Subsequence / Substring
DP – Matrix / Grid Path
DP – Interval / Matrix Chain Multiplication
DP – State Machine / Stock Problems
Bit Manipulation Pattern
Bit Manipulation
Use bitwise operations (AND, OR, XOR, shifts) for O(1) space tricks and O(n) solutions.
