Goal: Don’t memorise solutions. Memorise patterns. Most LeetCode questions are variations of these.
1. Hash Map / Hash Set
When to use
- Fast lookups
- Finding duplicates
- Counting frequencies
- Looking for complements (e.g. Two Sum)
Template
seen = {}
for i, x in enumerate(nums):
if target - x in seen:
return [seen[target - x], i]
seen[x] = iTime Complexity
- Time: O(n)
- Space: O(n)
Common Problems
- Two Sum
- Contains Duplicate
- Valid Anagram
- Group Anagrams
2. Two Pointers
When to use
- Sorted arrays
- Finding pairs
- Comparing from both ends
Template
l, r = 0, len(nums) - 1
while l < r:
if condition:
return
elif something:
l += 1
else:
r -= 1Time Complexity
- Time: O(n)
Common Problems
- Two Sum II
- Container With Most Water
- 3Sum
- Valid Palindrome
3. Sliding Window
When to use
- Subarrays
- Substrings
- “Longest”
- “Shortest”
- “Maximum”
Template
l = 0
for r in range(len(nums)):
# expand window
while window_invalid:
# shrink window
l += 1
# update answerTime Complexity
- Time: O(n)
Common Problems
- Longest Substring Without Repeating Characters
- Minimum Window Substring
- Best Time to Buy and Sell Stock
4. Binary Search
When to use
- Sorted arrays
- “Find”
- “First”
- “Last”
- “Minimum”
Template
l, r = 0, len(nums) - 1
while l <= r:
mid = (l + r) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
l = mid + 1
else:
r = mid - 1Time Complexity
- Time: O(log n)
Common Problems
- Binary Search
- Search Rotated Sorted Array
- Find Peak Element
5. Stack
When to use
- Matching brackets
- Undo operations
- Previous/Next Greater Element
Template
stack = []
for x in nums:
while stack and condition:
stack.pop()
stack.append(x)Common Problems
- Valid Parentheses
- Daily Temperatures
- Largest Rectangle in Histogram
6. Fast & Slow Pointers
When to use
- Linked Lists
- Detecting cycles
- Finding the middle
Template
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.nextCommon Problems
- Linked List Cycle
- Middle of Linked List
- Happy Number
7. DFS (Depth First Search)
When to use
- Trees
- Graphs
- Explore every path
Template
def dfs(node):
if not node:
return
dfs(node.left)
dfs(node.right)Common Problems
- Maximum Depth of Binary Tree
- Same Tree
- Path Sum
8. BFS (Breadth First Search)
When to use
- Level order traversal
- Shortest path (unweighted graph)
Template
from collections import deque
q = deque([root])
while q:
node = q.popleft()
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)Common Problems
- Binary Tree Level Order Traversal
- Rotting Oranges
- Word Ladder
9. Backtracking
When to use
- Generate every possibility
- Combinations
- Permutations
Template
res = []
def backtrack(path):
if finished:
res.append(path.copy())
return
for choice in choices:
path.append(choice)
backtrack(path)
path.pop()Common Problems
- Subsets
- Combination Sum
- Permutations
- N Queens
10. Heap (Priority Queue)
When to use
- Top K
- Smallest/Largest
- Scheduling
Template
import heapq
heap = []
for x in nums:
heapq.heappush(heap, x)
smallest = heapq.heappop(heap)Common Problems
- Kth Largest Element
- Top K Frequent Elements
- Merge K Sorted Lists
11. Intervals
When to use
- Merging ranges
- Scheduling
- Overlapping intervals
Template
intervals.sort()
res = []
for start, end in intervals:
if not res or start > res[-1][1]:
res.append([start, end])
else:
res[-1][1] = max(res[-1][1], end)Common Problems
- Merge Intervals
- Insert Interval
- Meeting Rooms
12. Dynamic Programming
When to use
Ask yourself:
“Can I solve this using smaller versions of the same problem?”
Template
dp = [0] * (n + 1)
dp[0] = base_case
for i in range(1, n + 1):
dp[i] = ...Common Problems
- Climbing Stairs
- House Robber
- Coin Change
- Longest Increasing Subsequence
13. Graph DFS
Template
visited = set()
def dfs(node):
if node in visited:
return
visited.add(node)
for nei in graph[node]:
dfs(nei)Common Problems
- Number of Islands
- Clone Graph
- Course Schedule
14. Graph BFS
Template
from collections import deque
visited = {start}
q = deque([start])
while q:
node = q.popleft()
for nei in graph[node]:
if nei not in visited:
visited.add(nei)
q.append(nei)Common Problems
- Shortest Path
- Number of Islands
- Word Ladder
15. Topological Sort
When to use
- Dependencies
- Prerequisites
- Ordering tasks
Template
from collections import defaultdict, deque
graph = defaultdict(list)
indegree = defaultdict(int)
# build graph
q = deque(node for node in nodes if indegree[node] == 0)
while q:
node = q.popleft()
for nei in graph[node]:
indegree[nei] -= 1
if indegree[nei] == 0:
q.append(nei)Common Problems
- Course Schedule
- Alien Dictionary
How to Identify the Right Pattern
If the question says… | Think… |
|---|---|
Find a pair | Two Pointers / Hash Map |
Duplicates | Hash Set |
Frequency | Hash Map |
Longest substring | Sliding Window |
Sorted array | Binary Search / Two Pointers |
Top K | Heap |
Tree | DFS / BFS |
Graph | DFS / BFS |
All combinations | Backtracking |
Merge overlapping ranges | Intervals |
Dependencies | Topological Sort |
Best/Maximum with repeated choices | Dynamic Programming |
My Interview Order
If you’re just starting LeetCode, learn these in order:
- ✅ Arrays & Hash Maps
- ✅ Two Pointers
- ✅ Sliding Window
- ✅ Stack
- ✅ Binary Search
- ✅ Linked Lists
- ✅ Trees (DFS & BFS)
- ✅ Heaps
- ✅ Graphs
- ✅ Backtracking
- ✅ Dynamic Programming
The Golden Rule
Don’t memorize 300 solutions.
Instead, train yourself to ask:
- Do I need fast lookup? → Hash Map
- Is the array sorted? → Two Pointers / Binary Search
- Is this about a subarray or substring? → Sliding Window
- Am I exploring a tree or graph? → DFS/BFS
- Do I need every possible answer? → Backtracking
- Do I only care about the biggest/smallest? → Heap
- Are there overlapping intervals? → Interval Merge
- Can this be built from smaller answers? → Dynamic Programming
If you can answer those questions, you’ll recognize the pattern behind most interview problems instead of trying to remember each solution individually.
As you work through problems, consider adding one representative LeetCode problem under each pattern (e.g., Two Sum for Hash Maps, Valid Parentheses for Stacks, Climbing Stairs for DP). Revisiting those 10–15 “anchor problems” is often far more effective than rereading dozens of solutions.