1. Two Pointers
⇄
Pattern 1
Two Pointers (Opposite Direction)
When to use: The input is sorted (or can be sorted), and you need to find a pair that satisfies some condition (sum, difference, palindrome check). Moving one pointer shrinks the search space.
Template — Opposite Pointers
two_pointers_opposite.py read-only
⇉
Pattern 1b
Two Pointers (Same Direction / Fast-Slow)
When to use: Remove duplicates in-place, detect cycles in a linked list, or partition an array. One pointer moves faster than the other.
Template — Fast-Slow Pointers
two_pointers_fast_slow.py read-only
Example Problems
- Two Sum II (sorted) — opposite pointers, O(n)
- Valid Palindrome — opposite pointers skip non-alnum
- Remove Duplicates from Sorted Array — fast-slow, O(1) space
2. Sliding Window
□
Pattern 2a
Fixed-Size Sliding Window
When to use: You need the max/min/sum of every contiguous subarray of exactly size k. Slide a window of fixed length across the array.
Template — Fixed Window
sliding_window_fixed.py read-only
⇔
Pattern 2b
Variable-Size Sliding Window
When to use: Find the smallest/longest subarray satisfying a condition. Expand the right boundary, then shrink the left boundary while the condition holds.
Template — Variable Window
sliding_window_variable.py read-only
Example Problems
- Maximum Average Subarray I — fixed window, O(n)
- Longest Substring Without Repeating Characters — variable window
- Minimum Window Substring — variable window with frequency map
3. Breadth-First Search (BFS)
↠
Pattern 3
BFS — Level-Order / Shortest Path
When to use: Shortest path in an unweighted graph, level-order tree traversal, or exploring nodes by distance. BFS guarantees the first time you reach a node is the shortest path.
Template — BFS with deque
bfs_template.py read-only
Example Problems
- Binary Tree Level Order Traversal — classic BFS
- Shortest Path in Binary Matrix — BFS on grid
- Rotting Oranges — multi-source BFS
4. Depth-First Search (DFS)
↓
Pattern 4
DFS — Recursive & Iterative
When to use: Explore all paths, detect cycles, count connected components, solve tree problems (height, diameter, path sum). Prefer recursion for trees; use an explicit stack for graphs to avoid stack overflow.
Template — DFS Recursive (Tree)
dfs_recursive.py read-only
Template — DFS Iterative (Graph)
dfs_iterative.py read-only
Example Problems
- Maximum Depth of Binary Tree — recursive DFS
- Number of Islands — DFS flood fill
- Course Schedule (cycle detection) — DFS with coloring
5. Binary Search
÷
Pattern 5a
Classic Binary Search
When to use: The input is sorted and you need to find an element or insertion point. The
while left <= right loop halves the search space each iteration.
Template — Classic Binary Search
binary_search_classic.py read-only
🔍
Pattern 5b
Binary Search on Answer
When to use: The problem asks for the minimum/maximum value that satisfies a condition. Binary search on the answer space and check feasibility.
Template — Search on Answer
binary_search_on_answer.py read-only
Example Problems
- Binary Search — classic template
- Koko Eating Bananas — binary search on answer
- Search in Rotated Sorted Array — modified binary search
6. Dynamic Programming
▦
Pattern 6
Dynamic Programming (1D & 2D)
When to use: The problem has overlapping subproblems and optimal substructure. If a brute-force recursion repeats work, DP caches it. Look for phrases like "minimum cost", "number of ways", "can you reach".
Template — 1D DP (Bottom-Up)
dp_1d.py read-only
Template — 2D DP
dp_2d.py read-only
Example Problems
- Climbing Stairs — 1D DP, Fibonacci variant
- Longest Common Subsequence — 2D DP
- Coin Change — 1D DP, unbounded knapsack
7. Backtracking
↩
Pattern 7
Backtracking — Choose, Explore, Unchoose
When to use: Generate all permutations, combinations, or subsets. The problem requires exploring a decision tree and pruning invalid branches. You need every valid configuration, not just one.
Template — Choose-Explore-Unchoose
backtracking_template.py read-only
Example Problems
- Permutations — classic backtracking
- Subsets — backtracking with start index
- N-Queens — constraint-based pruning
8. Greedy
✔
Pattern 8
Greedy — Local Optimal = Global Optimal
When to use: Making the locally optimal choice at each step leads to the globally optimal solution. Often involves sorting first. If you can prove a greedy choice property exists, skip DP.
Template — Interval Scheduling
greedy_intervals.py read-only
Example Problems
- Non-Overlapping Intervals — greedy by end time
- Jump Game — greedy farthest reach
- Assign Cookies — sort + two pointers greedy
Quick Reference Table
| Pattern | Signal Words | Time | Space | Top 3 Problems |
|---|---|---|---|---|
| Two Pointers | sorted, pair, palindrome | O(n) |
O(1) |
Two Sum II, Valid Palindrome, Remove Duplicates |
| Sliding Window | subarray, substring, at most k | O(n) |
O(k) |
Max Avg Subarray, Longest Substring, Min Window Substring |
| BFS | shortest path, level order, min steps | O(V+E) |
O(V) |
Level Order Traversal, Shortest Path in Grid, Rotting Oranges |
| DFS | all paths, connected components, tree depth | O(V+E) |
O(V) |
Max Depth, Number of Islands, Course Schedule |
| Binary Search | sorted, O(log n), minimize max | O(log n) |
O(1) |
Binary Search, Koko Eating Bananas, Search Rotated Array |
| Dynamic Programming | min cost, # of ways, longest subsequence | O(n*m) |
O(n*m) |
Climbing Stairs, LCS, Coin Change |
| Backtracking | all permutations, combinations, subsets | O(n!) |
O(n) |
Permutations, Subsets, N-Queens |
| Greedy | intervals, jump, activity selection | O(n log n) |
O(1) |
Non-Overlapping Intervals, Jump Game, Assign Cookies |
Decision tip: Start by asking — is the input sorted? (Binary search or two pointers.) Is it a graph/tree? (BFS for shortest path, DFS for all paths.) Do subproblems overlap? (DP.) Need all arrangements? (Backtracking.) Can a local choice always win? (Greedy.)
Africa & India's Best AI Academy
You've got the patterns.
Now get the career.
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →