Front
Back
Two Pointers (Pattern)
Uses two indices to traverse a data structure, often from opposite ends or at different speeds.
When to use Two Pointers
Finding pairs/triplets in sorted arrays, palindrome checks, removing duplicates. Optimizes from O(n²) to O(n).
Sliding Window (Pattern)
A contiguous sub-array/sub-string that “slides” (expands and contracts) across the data.
When to use Sliding Window
Finding longest/shortest contiguous substring/subarray, or max/min sum of a fixed size.
Prefix Sums (Pattern)
An array where each element is the cumulative sum (or product) up to that index.
When to use Prefix Sums
Quickly calculating the sum of multiple sub-ranges in O(1) time after O(n) preprocessing.
Cyclic Sort (Pattern)
In-place sort for arrays containing numbers in a specific range (e.g., 1 to N).
When to use Cyclic Sort
Finding missing numbers, duplicates, or corrupted numbers in a 1-to-N range array.
Fast & Slow Pointers (Pattern)
Uses two pointers (tortoise and hare) that move at different speeds.
When to use Fast & Slow Pointers
Detecting a cycle in a linked list, finding the middle element, or finding a “Happy Number”.
In-place Reversal (Pattern)
Reversing the links between nodes in a linked list without using extra space.
When to use In-place Reversal
Reversing a linked list or a sub-list.
Breadth-First Search (BFS)
Traverses a tree or graph level by level using a Queue.
When to use BFS
Finding the shortest path in an unweighted graph, level order traversal of a tree.
Depth-First Search (DFS)
Explores as far as possible along each branch before backtracking, using recursion or a Stack.
When to use DFS
Finding a path, checking connectivity, tree traversals (in-order, pre-order, post-order).
Backtracking (Pattern)
A systematic way to try all possible configurations, “backtracking” when a path is invalid.
When to use Backtracking
Problems involving permutations, combinations, or subsets (e.g., N-Queens, Sudoku).
Binary Search (Pattern)
Efficiently finds an element by repeatedly dividing the search interval in half.
When to use Binary Search
Searching in a sorted array or a sorted search space, in O(log n) time.
Two Heaps (Pattern)
Uses a Max Heap and a Min Heap to divide a set into two halves.
When to use Two Heaps
Finding the median of a stream of numbers.
Top ‘K’ Elements (Pattern)
Finds the K largest, smallest, or most frequent elements, often using a Min/Max Heap.