Solve fewer, learn deeper. Master patterns, not just problems.
How to use this list
Goal: internalize the patterns. For each pattern below: understand why it works, implement from scratch, then vary constraints.
- Timebox 25–35 minutes per problem; if stuck, skim hints and re‑implement cleanly without peeking.
- Repeat misses after 48 hours and 1 week.
- Write a one‑page “pattern card” per concept with invariants + pitfalls.
Core concepts & patterns
1) Two Pointers / Sliding Window
Why it works: For ordered data or contiguous constraints, move left/right bounds to maintain validity (e.g., at‑most‑K distinct, sum ≤ K) in O(n).
2) Binary Search (including Answer Space)
Use when a predicate over the domain is monotonic. Often search the answer with a feasibility check.
3) Hashing & Prefix Tricks
Map elements to counts/indices; use prefix sums, parity masks, rolling signatures to compress state.
4) Greedy
Prove a local optimal choice leads to the global optimum (exchange argument, interval structure).
5) Divide & Conquer
Split into smaller subproblems and merge (e.g., mergesort, selection, closest pair).
6) Dynamic Programming
Define subproblems, transitions, base cases, and evaluation order (1D/2D, subsequences, intervals).
7) Graphs (BFS/DFS/Topo/Shortest Path)
Connectivity, cycle detection, ordering constraints, shortest paths (weighted or not).
8) Trees & BSTs
Use recursion/iteration for traversals; leverage BST invariants for O(h) operations and in‑order sequencing.
9) Heaps / Priority Queues
Maintain top‑k, rolling medians, or greedy picks with O(log n) updates.
10) Backtracking
Systematically explore state space with pruning; template: choose → recurse → undo.
Must‑Solve LeetCode Question Set (by pattern)
Sliding Window
- Longest Substring Without Repeating Characters — expanding window + last‑seen map.
- Minimum Window Substring — “cover all requirements” window.
- Container With Most Water — two pointers; dominated heights argument.
Binary Search (+ Answer Space)
- Binary Search — clean implementation; careful with bounds.
- Find Minimum in Rotated Sorted Array — binary search on structure.
- Koko Eating Bananas — search speed; feasibility predicate.
Hashing & Prefix
- Two Sum — one‑pass map.
- Subarray Sum Equals K — prefix counts.
- Count Number of Nice Subarrays — at‑most‑k trick / parity prefix.
Greedy
- Jump Game — farthest reach invariant.
- Meeting Rooms II — sweep line / two arrays.
- Non‑overlapping Intervals — sort by end time.
Divide & Conquer
- Merge k Sorted Lists — heap or pairwise merge.
- Maximum Subarray — Kadane + D&C variant.
- Sort an Array — implement mergesort/quicksort.
Dynamic Programming
- House Robber — 1D DP with two states.
- Unique Paths — grid DP; combinatorics alt.
- Longest Increasing Subsequence — patience sorting O(n log n).
- Coin Change — unbounded knapsack template.
Graphs
- Number of Islands — grid DFS/BFS.
- Course Schedule — topo sort / cycle detection.
- Rotting Oranges — multi‑source BFS.
Trees & BSTs
- Binary Tree Level Order Traversal — BFS levels.
- Validate Binary Search Tree — bounds / in‑order.
- Lowest Common Ancestor of a Binary Tree — post‑order return up.
Heaps / Priority Queues
- Kth Largest Element in an Array — heap or quickselect.
- Top K Frequent Elements — heap/bucket.
- Find Median from Data Stream — two heaps.
Backtracking
- Combination Sum — unbounded choices + pruning.
- Permutations — choose / used / undo.
- Word Search — board DFS with visited.
4‑week practice plan (3–5 problems/day)
Sliding Window, Hashing/Prefix
Binary Search, Greedy, Heaps
Trees, Graphs, Recursion
Dynamic Programming + Mixed review
- Daily: 1 easy → 2 mediums → (opt) 1 hard/variant.
- Review misses after 48 hours, then 1 week.
- Write a quick “why it works” note for each solved problem.
Reusable snippets (pattern templates)
def longest_substring_k_distinct(s, k):
from collections import Counter
left = 0
freq = Counter()
best = 0
for right, ch in enumerate(s):
freq[ch] += 1
while len(freq) > k:
freq[s[left]] -= 1
if freq[s[left]] == 0:
del freq[s[left]]
left += 1
best = max(best, right - left + 1)
return best
def min_speed(piles, h):
import math
lo, hi = 1, max(piles)
def ok(speed):
return sum(math.ceil(p / speed) for p in piles) <= h
while lo < hi:
mid = (lo + hi) // 2
if ok(mid):
hi = mid
else:
lo = mid + 1
return lo
def subarray_sum_k(nums, k):
from collections import defaultdict
count = defaultdict(int)
count[0] = 1
s = res = 0
for x in nums:
s += x
res += count[s - k]
count[s] += 1
return res
Final interview checklist
- Clarify constraints and edge cases; state target complexity.
- Start with brute force → move to an optimal pattern.
- Walk through examples and maintain invariants out loud.
- Test extremes: empty/single/duplicates/negatives/large inputs.
- Discuss tradeoffs (time/space, precompute vs on‑the‑fly).
Recommended reading:
Top LeetCode Questions for FAANG — by Devshree Bharatia
Blind 75 Must Do Leetcode
0 Comments