15 / 16

Coding Interview Patterns

The ~20 patterns behind most coding rounds, with the code to recognize and write them.

0/20 studied
01Big-O and complexity analysis

Big-O says how time or memory grows with input size, ignoring constants: before coding, name n, state time and space, and check them against the input limits.

Core◇ Diagram</> Code
02Hash maps, sets and prefix sums

A hash map gives O(1) average lookup by key, so "have I seen X?", "how many X?" and, with prefix sums, "how many subarrays sum to k?" each take one O(n) pass.

Core◇ Diagram</> Code
03Prefix sums and difference arrays

Precompute running totals once in O(n) and any range sum is one subtraction, O(1); the mirror trick, a difference array, makes each range update O(1).

Core◇ Diagram</> Code
04Two pointers

Move two indexes through a sorted array, string or linked list so every step discards candidates for good: O(n) time, O(1) space, where brute force checks all O(n²) pairs.

Core◇ Diagram</> Code
05Sliding window

Keep a contiguous window over an array or string and update its state as the edges move; each element enters and leaves once, so subarray and substring scans drop from O(n²) to O(n).

Core◇ Diagram</> Code
06Binary search and search on the answer

Halve a sorted range each step to find a value, or the first point where a monotonic yes/no predicate flips, in O(log n) checks: ~30 even for a 10⁹-wide answer space.

Core◇ Diagram</> Code
07Linked lists

A linked list gives up index access to get O(1) insert and delete at a known node; interview problems are pointer surgery with three tools: a dummy head, in-place reversal and fast/slow pointers.

Core◇ Diagram</> Code
08Binary trees and BSTs

Most tree problems are one traversal: a DFS where each node combines what its children return, or a BFS level by level; in a BST, inorder visits the keys in sorted order.

Core◇ Diagram</> Code
09Stacks and monotonic stacks

A stack holds the items still waiting for an answer; a monotonic stack pops every item the new one beats, so 'next greater or smaller' problems drop from O(n²) to O(n).

Advanced◇ Diagram</> Code
10Heaps and top-K

A heap returns the smallest (or largest) item in O(1) and inserts or removes in O(log n); a size-K heap gives top-K in O(n log K) without sorting everything.

Core◇ Diagram</> Code
11Interval problems: sort and sweep

Sort intervals by start, then sweep left to right comparing each one with the last kept interval; for 'how many at once', sweep +1/−1 start and end events instead.

Core◇ Diagram</> Code
12Graph traversal: BFS and DFS

BFS explores level by level with a queue and finds shortest paths when every edge costs the same; DFS goes deep with a stack or recursion; with a visited set, both are O(V + E).

Core◇ Diagram</> Code
13Topological sort

Order a directed graph so every edge points forward: repeatedly take a node with no remaining prerequisites (Kahn), or reverse the DFS finish order; nodes left over mean a cycle.

Core◇ Diagram</> Code
14Union-Find (Disjoint Set Union)

Track groups as trees with one root each: `find` returns the root, `union` links two roots, and with path compression plus union by rank both run in near-constant amortized time.

Advanced◇ Diagram</> Code
15Shortest Paths (BFS, Dijkstra, Bellman-Ford)

Choose by edge weights: BFS when all edges cost the same, Dijkstra with a min-heap when weights are non-negative, Bellman-Ford when some are negative or you must detect a negative cycle.

Core◇ Diagram</> Code
16Dynamic Programming

When the best answer is built from best answers to smaller subproblems, and those subproblems repeat, solve each one once and store it: exponential recursion becomes polynomial.

Core◇ Diagram</> Code
17Greedy algorithms

Take the locally best choice at each step and never undo it; it is correct only when you can prove, usually with an exchange argument, that the choice never rules out an optimal solution.

Core◇ Diagram</> Code
18Backtracking

Build a candidate one choice at a time, recurse, then undo the choice, abandoning any partial candidate that can't lead to a valid answer: the way to enumerate subsets, permutations and constraint puzzles.

Core◇ Diagram</> Code
19Tries (Prefix Trees)

A tree where each edge is a character and each path from the root spells a prefix, so insert, lookup and "any word starting with...?" cost O(key length), however many words are stored.

Core◇ Diagram</> Code
20Design a data structure (LRU cache)

In a "design a class" round no single structure makes every operation O(1), so you compose two: a hash map to find things plus a list, linked list or stack that keeps the order.

Advanced◇ Diagram</> Code