DSA Patterns That Actually Matter in Interviews
Stop memorizing solutions. Learn to recognize the 12 patterns that underpin 90% of coding interview questions.
Grinding 500 LeetCode problems isn't a strategy — it's a superstition. The developers who consistently pass technical screens don't have every solution memorized. They recognize the structural patterns that problems belong to, then apply the right template. This article breaks down the patterns that actually appear, with examples and the signal to watch for.
view_week Pattern 1 — Sliding Window
What it solves: Contiguous subarray problems — max sum, longest substring with constraint, minimum window.
Recognition signal: The problem involves a contiguous portion of an array/string, and you need to find something optimal about it.
# Maximum sum subarray of size k — O(n) def max_sum_subarray(arr, k): window_sum = sum(arr[:k]) best = window_sum for i in range(k, len(arr)): window_sum += arr[i] - arr[i - k] best = max(best, window_sum) return best
compare_arrows Pattern 2 — Two Pointers
What it solves: Pair search in sorted arrays, removing duplicates, palindrome checks, container with most water.
Recognition signal: Sorted input (or sortable), and you need to find pairs or partition in-place without extra space.
# Two Sum II (sorted array) — O(n) def two_sum_sorted(nums, target): lo, hi = 0, len(nums) - 1 while lo < hi: s = nums[lo] + nums[hi] if s == target: return [lo+1, hi+1] elif s < target: lo += 1 else: hi -= 1
stacks Pattern 3 — Monotonic Stack
What it solves: Next Greater Element, largest rectangle in histogram, daily temperatures, trapping rain water.
Recognition signal: The problem asks about the next/previous larger or smaller element for each position.
# Next Greater Element — O(n) def next_greater(nums): result = [-1] * len(nums) stack = [] # stores indices for i, n in enumerate(nums): while stack and nums[stack[-1]] < n: result[stack.pop()] = n stack.append(i) return result
account_tree Pattern 4 — BFS / Level-Order Tree
What it solves: Shortest path in unweighted graphs, level-order traversal, multi-source BFS, word ladder.
Recognition signal: You need the shortest path or are processing nodes level by level.
from collections import deque def bfs(root): if not root: return [] result, queue = [], deque([root]) while queue: level = [] for _ in range(len(queue)): node = queue.popleft() level.append(node.val) if node.left: queue.append(node.left) if node.right: queue.append(node.right) result.append(level) return result
linear_scale Pattern 5 — Binary Search on Answer
What it solves: "Find minimum X such that condition holds" — capacity problems, Koko eating bananas, split array largest sum.
Recognition signal: The problem has a monotonic feasibility condition — if X works, then X+1 also works (or vice versa).
# Template: find smallest k where feasible(k) is True def binary_search_answer(lo, hi, feasible): while lo < hi: mid = (lo + hi) // 2 if feasible(mid): hi = mid else: lo = mid + 1 return lo
tips_and_updates How to Practice
- Do 3–5 problems per pattern, not 50 random problems per day.
- After solving, ask: what was the key insight? Write it in one sentence.
- Re-solve the problem from memory 48 hours later. Spaced repetition is the actual secret.
- When you're stuck, map the problem to a pattern before looking at hints.
"The goal isn't to have solved this exact problem — it's to recognize which category it belongs to."
Pattern recognition is a trainable skill, not an innate talent.