Essential Coding Patterns
Master these essential patterns to solve 80% of coding interview problems. Recognizing the right pattern is half the battle!
Two Pointers
O(n)Use two pointers moving towards each other or in the same direction to solve array/string problems efficiently.
Sliding Window
O(n)Maintain a window of elements and slide it through the array to find optimal solutions.
Fast & Slow Pointers
O(n)Use two pointers moving at different speeds to detect cycles or find middle elements.
Merge Intervals
O(n log n)Merge or process overlapping intervals by sorting and comparing boundaries.
Cyclic Sort
O(n)Sort arrays containing numbers in a given range by placing each number at its correct index.
In-place Reversal of Linked List
O(n)Reverse linked lists or portions of them by manipulating pointers in-place.
Tree BFS (Breadth First Search)
O(n)Traverse tree level by level using a queue to process nodes.
Tree DFS (Depth First Search)
O(n)Traverse tree paths from root to leaves using recursion or stack.
Two Heaps
O(log n) insertUse two heaps (min and max) to efficiently find median or balance elements.
Subsets
O(2^n)Generate all possible combinations using BFS or backtracking.
Modified Binary Search
O(log n)Apply binary search on sorted or rotated arrays with modifications.
Top K Elements
O(n log k)Use heap or quickselect to find K largest/smallest elements efficiently.
K-way Merge
O(n log k)Merge K sorted arrays/lists using a heap to track smallest elements.
Dynamic Programming (0/1 Knapsack)
O(n * capacity)Choose or skip items to maximize/minimize value using memoization or tabulation.
Dynamic Programming (Unbounded Knapsack)
O(n * capacity)Similar to 0/1 but items can be used unlimited times.
Dynamic Programming (LCS/LIS)
O(n * m)Find longest common subsequence or increasing subsequence using DP.
Topological Sort
O(V + E)Order graph nodes where dependencies come before dependents.
Monotonic Stack
O(n)Maintain stack elements in monotonic order to find next greater/smaller elements.
Backtracking
ExponentialExplore all possibilities by building solutions incrementally and backtracking on failure.
Union Find (Disjoint Set)
O(α(n)) ≈ O(1)Track connected components and efficiently merge sets.
Recursion
Varies (O(n) to O(2^n))Break down problems into smaller subproblems by having functions call themselves with simpler inputs.