Interview Prep
A pattern-organized interview library: every problem is grouped by the algorithmic family it teaches (arrays & hashing, two pointers, sliding window, trees, graphs, DP, ...) and tagged with a difficulty tier — warm-up, standard, hard, or legendary. Every entry is solved in Python with the brute-force-then-optimal walkthrough you would actually deliver in a real interview. The goal is fluency, not memorisation.
Why Python: it has become the lingua franca for whiteboard interviews.
Dict and set literals are unbeatable for the patterns we need;
collections.Counter, heapq, and the slice
syntax let solutions compress to the algorithmic essentials without
Java-style ceremony.
The ML Engineering section at the bottom is the parallel track: stable softmax, gradients, attention, normalization, PCA — the numerical building blocks every ML candidate should be able to write from a cold start.
Arrays & Hashing
Indexing and presence checks. Hashmaps and sets as the "have I seen this before?" primitive.
- Warm-up Contains Duplicate arrayshashset
Decide if any value appears at least twice. Hashset as a presence detector.
- Warm-up Group Anagrams stringshashmap
Partition a list of strings into anagram buckets.
- Warm-up Longest Consecutive Sequence arrayshashset
Longest run of consecutive integers in an unsorted array in O(n). The "only start counting at sequence heads" trick.
- Warm-up Product of Array Except Self arraysprefix-products
Compute "product of all other elements" without division and in O(n).
- Warm-up Top K Frequent Elements heaphashmap
Return the K most frequent values without sorting everything.
- Warm-up Two Sum arrayshashmap
Find indices of two numbers summing to a target. The "have I seen the complement?" pattern.
- Warm-up Valid Anagram stringscounting
Decide if two strings are permutations of each other.
- Warm-up Valid Sudoku matrixhashset
Decide whether a partially filled Sudoku board violates any rule.
Two Pointers
Two indices sweeping a sorted (or sortable) array — opposite ends, fast/slow, or partition style.
- Warm-up Container With Most Water arraystwo-pointers
Find two lines that form the container holding the most water. Two-pointer convergence on a sorted-shape argument.
- Standard 3Sum arraystwo-pointerssorting
Find all unique triples that sum to zero. Sort + two-pointer extension of Two Sum.
- Hard Trapping Rain Water arraystwo-pointers
Total water trapped between elevation bars. Famous for having five elegant solutions.
Sliding Window
A moving window over an array or string, expanding and contracting to maintain an invariant.
- Warm-up Best Time to Buy and Sell Stock arraysone-pass
Maximize profit from one buy-then-sell. Single pass with a running minimum.
- Standard Longest Substring Without Repeating Characters stringssliding-window
The canonical sliding-window problem. Maintain a moving window of distinct chars.
- Hard Sliding Window Maximum arraysmonotonic-deque
Maximum of every length-k window in an array. The monotonic-deque pattern.
- ↳ Hard Shortest Subarray with Sum ≥ K Shortest contiguous subarray with sum ≥ K, allowing negative numbers. Monotonic deque on prefix-sum indices — sliding window fails because of the negatives.
- ↳ Hard Sliding Window Minimum Minimum of every length-k window. Identical to Sliding Window Maximum with the comparison flipped — but several other DP problems reduce to it.
Stack
LIFO bookkeeping. Matching, monotonic stacks, and "nearest greater/smaller" patterns.
- Warm-up Min Stack stackdesign
A stack supporting push, pop, top, and getMin all in O(1). The auxiliary-stack trick.
- Warm-up Valid Parentheses stackstrings
Decide whether a string of brackets is properly nested. The canonical stack problem.
- Hard Largest Rectangle in Histogram stackmonotonic-stack
Largest axis-aligned rectangle under a histogram in O(n). The reference monotonic-stack problem.
Binary Search
Halving the search space on a sorted (or monotone-predicate) input. The most over-rated and under-mastered primitive.
- Standard Search in Rotated Sorted Array binary-searchinvariants
Find a target in a sorted array that has been rotated by an unknown amount. Modified binary search.
- Legendary Median of Two Sorted Arrays binary-searchinvariants
Find the median of two sorted arrays in O(log min(m, n)). The famous "I can’t do this on the whiteboard" Google problem.
Linked List
Pointer juggling with dummy heads, fast/slow runners, and in-place reversal.
- Warm-up Merge Two Sorted Lists linked-listpointers
Merge two sorted linked lists into one sorted list. The dummy-node pattern.
- Warm-up Reverse Linked List linked-listpointers
Reverse a singly linked list in place. The pointer-juggling primitive.
- Hard Merge K Sorted Lists heaplinked-list
Merge k sorted linked lists into one sorted list. Min-heap of heads.
- Legendary LRU Cache designlinked-listhashmap
O(1) get and put for a fixed-size cache. Build the doubly-linked-list-plus-hashmap from scratch.
Trees
Recursion on binary trees — pre/in/post order, BFS, recursion with bounds, tree DP.
- Warm-up Maximum Depth of Binary Tree treesrecursion
Compute the maximum depth of a binary tree. Recursive tree fundamentals.
- ↳ Warm-up Balanced Binary Tree Decide whether |depth(left) − depth(right)| ≤ 1 at every node. Single post-order; propagate −1 upward as soon as imbalance is detected.
- ↳ Warm-up Minimum Depth of Binary Tree Minimum root-to-leaf depth. The "one missing child" trap — min(depth(left), depth(right)) silently returns 0 when one side is empty.
- ↳ Standard Diameter of Binary Tree Longest path between any two nodes. At each node, the through-path is depth(left) + depth(right); compute both in one post-order traversal.
- Standard Binary Tree Level Order Traversal treesbfs
Return the values of a binary tree level by level. The canonical BFS-on-a-tree problem.
- Standard Lowest Common Ancestor of a Binary Tree treesrecursion
Find the deepest node that has both p and q in its subtree. Elegant 5-line recursion once you see it.
- Standard Validate Binary Search Tree treesrecursioninvariants
Decide whether a binary tree is a valid BST. The classic "recursion with bounds" pattern.
- ↳ Standard Kth Smallest Element in a BST In-order traversal of a BST yields values in sorted order. Stop on the k-th visit. O(h + k).
- ↳ Standard Range Sum / Count in a BST Sum (or count) the values in [L, R] in a BST. Bounds-pruning recursion — skip whole subtrees that fall outside the range.
- ↳ Hard Recover Binary Search Tree Two BST nodes have been swapped. Restore the BST by swapping their values. In-order traversal finds the two out-of-order points.
- Hard Binary Tree Maximum Path Sum treesdp
Maximum sum of any path (need not pass through root). The "return one, track another" tree-DP pattern.
- Legendary Serialize and Deserialize Binary Tree treesdesigntraversal
Encode a binary tree to a string and decode it back. Demonstrates traversal + parsing in one problem.
Trie
Prefix-indexed dictionaries. The right structure when "all words starting with..." is the bottleneck.
Heap / Priority Queue
Top-k, k-way merges, streaming medians. Min/max-heap as the streaming-priority primitive.
- Standard Kth Largest Element heapquickselect
Find the k-th largest in O(n) average with quickselect, or O(n log k) with a min-heap. Two solutions worth knowing.
- Hard Find Median from Data Stream heapdesignstreaming
Support add(x) and findMedian() on a growing stream. Two heaps split below/above the median.
Backtracking
Decision trees explored by depth-first search with explicit undo. Subsets, permutations, constraint satisfaction.
- Warm-up Generate Parentheses backtrackingrecursion
Generate all well-formed parenthesis strings of length 2n. The canonical "count remaining" backtracking.
- Standard Permutations backtrackingrecursion
Generate all n! permutations of an array. Backtracking with a "used" set; sibling of Subsets.
- Standard Subsets (Backtracking) backtrackingrecursion
Generate all 2^n subsets of an array. The reference template for every backtracking problem.
- Standard Word Search (Grid Backtracking) backtrackinggriddfs
Decide whether a word can be spelled by a path through neighboring cells. DFS with mark-and-unmark backtracking.
Graphs
BFS/DFS on grids and graphs, topological sort, shortest paths.
- Standard Course Schedule graphstopological-sortcycle-detection
Decide if all courses can be completed given prerequisites. Topological sort with cycle check.
- Standard Number of Islands graphsdfsgrid
Count connected components of land cells in a grid. Grid-DFS in its purest form.
- Hard Word Ladder graphsbfsshortest-path
Find the shortest transformation sequence between two words via one-letter swaps. BFS on an implicit graph.
Dynamic Programming
Optimal substructure. From linear recurrences (Fibonacci-shaped) up to 2D table DPs on strings.
- Warm-up Climbing Stairs dpfibonacci
Count ways to climb n stairs taking 1 or 2 at a time. The simplest non-trivial DP.
- Warm-up House Robber dprecurrence
Max non-adjacent subarray sum. The simplest linear DP after Fibonacci.
- Warm-up Maximum Subarray arraysdpkadane
Find the contiguous subarray with the largest sum. Kadane's algorithm in 4 lines.
- ↳ Warm-up Kadane's Algorithm with Indices Return the indices of the max subarray, not just the sum. Track start/end alongside the running current sum; reset start on restart.
- ↳ Standard Maximum Product Subarray Track both min AND max running products. A large negative times another negative becomes a large positive — the min can flip the result.
- ↳ Standard Maximum Subarray (Circular) Allow the subarray to wrap around the end. Trick: max(Kadane on array, total − Kadane on negated array). Edge case: all-negative.
- Standard Coin Change dpunbounded-knapsack
Minimum coins to make a target amount. Greedy fails; DP nails it.
- Standard Word Break dpstrings
Decide whether a string can be segmented into a sequence of dictionary words. DP over prefix lengths.
- Hard Edit Distance dpstrings
Minimum edits to turn one string into another. The textbook 2D DP.
- Hard Longest Palindromic Substring stringsdp
Find the longest palindrome substring. Center-expansion in O(n²); Manacher in O(n).
- Legendary Regular Expression Matching dpstrings
Implement regex matching with . and *. 2D DP that everyone gets wrong on the first try.
Intervals
Sort by start, sweep, merge. The intervals canon.
ML Engineering
Numerical building blocks every ML engineer should be able to write: softmax, gradients, attention, normalization, PCA, sampling.
- Warm-up Cross-Entropy + Softmax Gradient mlderivativesnumerics
Derive the famous (p − y) gradient of softmax + cross-entropy and explain why fusing them is critical for numerical stability.
- Warm-up Online Mean and Variance (Welford) mlstreamingnumerics
Compute running mean and variance in one pass without numerical instability. The Welford recurrence.
- Warm-up Reservoir Sampling mlstreamingprobability
Sample k items uniformly from a stream of unknown length. Foundational streaming ML.
- Warm-up Sigmoid and Its Gradient mlderivatives
Implement the sigmoid, compute its derivative, and explain why σ(x)(1−σ(x)) makes backprop cheap.
- Warm-up Stable Softmax mlnumerics
Implement softmax without overflowing on large logits. The log-sum-exp trick.
- Standard Backprop a 2-Layer MLP mlautogradnumpy
Forward + backward pass on a 2-layer MLP by hand. The autograd primitive every ML candidate should be able to write from scratch.
- Standard Dropout From Scratch mlregularizationnumpy
Implement inverted dropout (forward + backward) and explain why the scale factor lives in training, not inference.
- Standard K-Means From Scratch mlclusteringnumpy
Implement Lloyd's algorithm in pure NumPy. The unsupervised baseline.
- Standard Layer Normalization mlnumpytransformer
Implement layer norm (forward + backward) and explain how it differs from batch norm. Foundational transformer component.
- Hard Attention From Scratch mltransformernumpy
Implement scaled dot-product attention and a single-head self-attention block. The transformer's load-bearing primitive.
- Hard Multi-Head Attention mltransformernumpy
Extend single-head attention to multi-head with split/concat. The complete transformer attention block.
- Legendary PCA From Scratch mllinear-algebranumpy
Implement PCA via SVD: center, decompose, project. The dimensionality-reduction primitive every ML engineer must know.