LeetCode Patterns: Core Problems Guide
Table of Contents
Section titled “Table of Contents”- How to Use This Guide
- 1. Arrays / Hashing
- 2. Sliding Window / Strings
- 3. Binary Search
- 4. Trees / Graphs
- 5. Linked List
- 6. Intervals / Greedy
- 7. Dynamic Programming
- 8. Backtracking
- Practice Plan
LeetCode Patterns: Core Problems Guide
Section titled “LeetCode Patterns: Core Problems Guide”Companion solutions:
Companion PDFs:
How to Use This Guide
Section titled “How to Use This Guide”For each problem:
- Identify the pattern first.
- Write brute force quickly.
- Replace with optimal pattern.
- State time and space complexity out loud.
1. Arrays / Hashing
Section titled “1. Arrays / Hashing”1. Two SumPattern: hash map for complement lookup. Complexity:O(n)time,O(n)space.49. Group AnagramsPattern: hash by character frequency or sorted signature. Complexity:O(n * k log k)with sorting,O(n * k)with count signature.238. Product of Array Except SelfPattern: prefix and suffix products without division. Complexity:O(n)time,O(1)extra space (excluding output).560. Subarray Sum Equals KPattern: prefix sum + hash map of seen sums. Complexity:O(n)time,O(n)space.347. Top K Frequent ElementsPattern: frequency map + bucket sort (or heap). Complexity:O(n)average with buckets,O(n log k)with heap.
2. Sliding Window / Strings
Section titled “2. Sliding Window / Strings”3. Longest Substring Without Repeating CharactersPattern: variable window + set/map for last seen index. Complexity:O(n)time,O(min(n, charset))space.76. Minimum Window SubstringPattern: variable window + frequency counters + valid-match count. Complexity:O(n)time,O(charset)space.424. Longest Repeating Character ReplacementPattern: window + max frequency in window. Complexity:O(n)time,O(charset)space.567. Permutation in StringPattern: fixed-size sliding window + frequency compare. Complexity:O(n)time,O(charset)space.125. Valid PalindromePattern: two pointers skipping non-alphanumeric. Complexity:O(n)time,O(1)extra space.
3. Binary Search
Section titled “3. Binary Search”33. Search in Rotated Sorted ArrayPattern: binary search with sorted-half detection. Complexity:O(log n)time,O(1)space.153. Find Minimum in Rotated Sorted ArrayPattern: binary search using right boundary comparison. Complexity:O(log n)time,O(1)space.875. Koko Eating BananasPattern: binary search on answer (minimum feasible speed). Complexity:O(n log m)wheremis max pile.981. Time Based Key-Value StorePattern: hash map key to sorted(timestamp, value)list + binary search. Complexity:set O(1),get O(log n)per key.
4. Trees / Graphs
Section titled “4. Trees / Graphs”102. Binary Tree Level Order TraversalPattern: BFS with queue by level. Complexity:O(n)time,O(w)space (tree width).199. Binary Tree Right Side ViewPattern: BFS by level or DFS right-first. Complexity:O(n)time,O(h)DFS orO(w)BFS space.236. Lowest Common Ancestor of a Binary TreePattern: postorder DFS returns node or null. Complexity:O(n)time,O(h)space.200. Number of IslandsPattern: grid DFS/BFS flood fill. Complexity:O(m*n)time,O(m*n)worst-case stack/queue.133. Clone GraphPattern: DFS/BFS + old-to-new node map. Complexity:O(V+E)time,O(V)space.
5. Linked List
Section titled “5. Linked List”206. Reverse Linked ListPattern: iterative pointer reversal. Complexity:O(n)time,O(1)space.21. Merge Two Sorted ListsPattern: two-pointer merge with dummy head. Complexity:O(n+m)time,O(1)extra space.143. Reorder ListPattern: find middle + reverse second half + merge alternating. Complexity:O(n)time,O(1)space.141. Linked List CyclePattern: Floyd slow/fast pointers. Complexity:O(n)time,O(1)space.2. Add Two NumbersPattern: digit-by-digit sum with carry. Complexity:O(max(n,m))time,O(1)extra space.
6. Intervals / Greedy
Section titled “6. Intervals / Greedy”56. Merge IntervalsPattern: sort by start, merge overlaps. Complexity:O(n log n)time,O(n)output space.57. Insert IntervalPattern: append non-overlap left, merge overlap, append right. Complexity:O(n)time,O(n)output space.435. Non-overlapping IntervalsPattern: greedy by earliest end time; count removals. Complexity:O(n log n)time,O(1)extra space after sort.452. Minimum Number of Arrows to Burst BalloonsPattern: greedy by end coordinate. Complexity:O(n log n)time,O(1)extra space after sort.
7. Dynamic Programming
Section titled “7. Dynamic Programming”70. Climbing StairsPattern: Fibonacci-style 1D DP. Complexity:O(n)time,O(1)space optimized.198. House RobberPattern: take/skip state transition. Complexity:O(n)time,O(1)space optimized.322. Coin ChangePattern: unbounded knapsack minimum coins. Complexity:O(amount * coins)time,O(amount)space.139. Word BreakPattern: DP over prefix breakability. Complexity:O(n^2)typical, depends on dictionary lookup/trie.300. Longest Increasing SubsequencePattern: patience sorting tails + binary search. Complexity:O(n log n)time,O(n)space.
8. Backtracking
Section titled “8. Backtracking”39. Combination SumPattern: choose/recurse with repeat allowed. Complexity: exponential search space.46. PermutationsPattern: backtracking with used-set / in-place swap. Complexity:O(n * n!)time.78. SubsetsPattern: include/exclude recursion. Complexity:O(n * 2^n)time.79. Word SearchPattern: DFS + visited marking + backtrack. Complexity:O(m*n*4^L)worst-case.
Practice Plan
Section titled “Practice Plan”- Week 1: Arrays/Hashing + Sliding Window.
- Week 2: Binary Search + Linked List.
- Week 3: Trees/Graphs + Intervals/Greedy.
- Week 4: DP + Backtracking + timed mixed sets.
Interview prep target:
- Solve each problem twice.
- On second pass, explain pattern and complexity in under 60 seconds.