Cracking ML Interviews

Open in the interactive study hub

ML Interview Cheat Sheet

The night-before quick reference: formulas, tables and the numbers interviewers expect you to know cold. No derivations, no narrative. Every section ends with a link to the guide that explains it properly.

Notation: n = samples, d = features, k = classes/clusters/top-k, p = predicted probability, y = label, ŷ = prediction, η = learning rate, log = natural log unless stated.

Jump to: Classification · Regression · Ranking · Losses · Prob & Stats · Lin Alg & Calculus · Optimization · Algorithms · Bias-Variance · Neural Nets · LLM Numbers · System Numbers · Top 20 Traps


Classification Metrics

                  Predicted
                Neg      Pos
Actual  Neg  |  TN   |   FP   |   FP = false alarm (Type I)
        Pos  |  FN   |   TP   |   FN = miss (Type II)
Metric Formula Use when
Accuracy (TP+TN) / (TP+TN+FP+FN) Balanced classes, symmetric costs
Precision (PPV) TP / (TP+FP) False positives are expensive (spam filter, alerts)
Recall (TPR, sensitivity) TP / (TP+FN) Misses are expensive (cancer, fraud)
Specificity (TNR) TN / (TN+FP) Clearing negatives matters; FPR = 1 - TNR
F1 2PR / (P+R) = 2TP / (2TP+FP+FN) One number, imbalanced data
F-beta (1+β²)·P·R / (β²·P + R) β>1 weights recall, β<1 weights precision
Balanced accuracy (TPR + TNR) / 2 Imbalanced, both classes matter
MCC (TP·TN - FP·FN) / √((TP+FP)(TP+FN)(TN+FP)(TN+FN)) Honest single score in [-1, 1]
Log loss (BCE) -(1/n) Σ [y log p + (1-y) log(1-p)] Probabilities feed decisions; punishes confident errors
Brier score (1/n) Σ (p - y)² Calibration + sharpness; bounded [0, 1]
ROC-AUC PR-AUC (average precision)
Axes TPR vs FPR Precision vs Recall
Meaning P(score(random pos) > score(random neg)) Precision averaged over recall levels
Random baseline 0.5 Prevalence π (e.g. 0.01 at 1% positives)
Class balance Invariant to prevalence Sensitive to prevalence
Prefer when Balanced data; care about ranking both classes Rare positives; you only act on the top of the list

Which metric when:

Read more: Model Evaluation · Anomaly Detection


Regression Metrics

Metric Formula Optimal constant / notes
MSE (1/n) Σ (y - ŷ)² Minimised by the mean; outlier-sensitive
RMSE √MSE Same units as y
MAE (1/n) Σ |y - ŷ| Minimised by the median; robust
MAPE (100/n) Σ |(y - ŷ) / y| Undefined at y=0; penalises over-forecasts more (unbounded) than under-forecasts (capped at 100%)
sMAPE (100/n) Σ |y - ŷ| / ((|y| + |ŷ|)/2) Bounded [0, 200%]; still unstable near 0
RMSLE √((1/n) Σ (log(1+ŷ) - log(1+y))²) Relative errors; penalises under-prediction more
R² 1 - SS_res / SS_tot Fraction of variance explained; can be negative on test data
Adjusted R² 1 - (1-R²)(n-1)/(n-p-1) Penalises adding features (p = #predictors)
Pinball (quantile τ) mean(max(τ·(y-ŷ), (τ-1)·(y-ŷ))) Minimised by the τ-quantile; prediction intervals
MASE MAE / MAE of naive (seasonal) forecast Time series; scale-free; < 1 beats naive

Read more: Model Evaluation · Time Series


Ranking Metrics

Metric Formula Notes
Precision@k (# relevant in top k) / k Ignores order within top k
Recall@k (# relevant in top k) / (total # relevant) Retrieval stage of a recsys/RAG
Hit rate@k 1 if any relevant in top k else 0, averaged One correct answer per query
MRR (1/|Q|) Σ_q 1 / rank_q rank_q = position of first relevant item
AP (per query) (1/R) Σ_{i=1..k} P@i · rel_i R = # relevant items; rel_i ∈ {0,1}
MAP mean of AP over queries Binary relevance, rewards putting all hits early
DCG@k Σ_{i=1..k} (2^rel_i - 1) / log2(i + 1) Graded relevance; some use gain rel_i instead of 2^rel_i - 1
NDCG@k DCG@k / IDCG@k IDCG = DCG of the ideal ordering; in [0, 1]

Read more: Model Evaluation · Recommender Systems · Search Ranking System


Loss Functions

Loss Formula When used
MSE / L2 (1/n) Σ (y - ŷ)² Regression, Gaussian noise; gradient ∝ (ŷ - y)
MAE / L1 (1/n) Σ |y - ŷ| Regression with outliers, Laplace noise; gradient is ±1 (non-smooth at 0)
Huber (δ) ½r² if |r| ≤ δ, else δ(|r| - ½δ), r = y - ŷ Robust regression; smooth version of L1
Binary cross-entropy -[y log p + (1-y) log(1-p)], p = σ(z) Binary / multi-label classification
Categorical CE -Σ_c y_c log p_c, p = softmax(z) Multiclass, LM next-token prediction
Label-smoothed CE CE with target (1-ε)·y + ε/K Reduce over-confidence; classification, MT
Focal -α_t (1 - p_t)^γ log p_t Heavy class imbalance (dense detection); γ≈2 down-weights easy examples
Hinge max(0, 1 - y·f(x)), y ∈ {-1, +1} SVMs; margin-based, not probabilistic
Contrastive (pairwise) y·d² + (1-y)·max(0, m - d)² Siamese nets; y=1 similar pair, d = distance, m = margin
InfoNCE -log( exp(s(q,k⁺)/τ) / Σ_j exp(s(q,k_j)/τ) ) CLIP, SimCLR, dense retrieval; in-batch negatives, temperature τ
Triplet max(0, d(a,p) - d(a,n) + m) Face ID, metric learning; needs hard-negative mining
KL divergence KL(P‖Q) = Σ P(x) log(P(x)/Q(x)) Distillation, VAEs, RLHF penalty; ≥ 0, not symmetric

Read more: Neural Network Training · Embeddings · Model Compression


Probability and Statistics

Core rules

Distribution Mean Variance Typical use
Bernoulli(p) p p(1-p) Single click / conversion
Binomial(n, p) np np(1-p) # successes in n trials
Geometric(p) (trials to 1st success) 1/p (1-p)/p² Attempts until success
Poisson(λ) λ λ Counts per interval (arrivals, events)
Uniform(a, b) (a+b)/2 (b-a)²/12 Random init, priors
Normal(μ, σ²) μ σ² Sums/means (CLT), noise
Exponential(λ) 1/λ 1/λ² Waiting time; memoryless
Gamma(k, θ) (shape, scale) kθ kθ² Sum of k exponentials; positive skew
Beta(α, β) α/(α+β) αβ / ((α+β)²(α+β+1)) Prior on a rate (Thompson sampling)

Inference

Test Use when
z-test Means with known σ or large n; comparing two proportions (CTR, conversion)
t-test (one/two-sample) Means, unknown σ, small-to-moderate n; Welch if variances differ
Paired t-test Same units measured twice (before/after, same queries under two models)
Chi-square Categorical counts: independence (contingency table) or goodness of fit; expected counts ≥ 5
Mann-Whitney U Two groups, non-normal / ordinal, no mean assumption
ANOVA Means across > 2 groups
Bootstrap Any statistic (median, AUC, ratio metrics) without a closed-form SE

A/B test sample size (per arm, two-sided):

n ≈ (z_{1-α/2} + z_{1-β})² · 2σ² / δ²
α = 0.05, power = 0.8  →  (1.96 + 0.84)² ≈ 7.85  →  n ≈ 16·σ² / δ²
binary metric: σ² = p(1-p)
Multiple-testing correction Controls Rule (m tests)
Bonferroni FWER Reject if p < α/m; simple, conservative
Holm (step-down) FWER Sort p ascending; reject while p_(i) ≤ α/(m - i + 1); always at least as powerful as Bonferroni
Benjamini-Hochberg FDR Sort ascending; largest i with p_(i) ≤ (i/m)·α; reject hypotheses 1..i

Read more: Statistics and Probability · A/B Testing · Causal Inference


Linear Algebra and Calculus

Derivatives to know

Function Derivative
Sigmoid σ(x) = 1/(1 + e^{-x}) σ(x)·(1 - σ(x)), max 0.25 at x = 0
tanh(x) 1 - tanh²(x), max 1 at x = 0
ReLU max(0, x) 1 if x > 0, else 0
Softmax p_i = e^{z_i} / Σ_j e^{z_j} ∂p_i/∂z_j = p_i(δ_ij - p_j)
Softmax + CE loss ∂L/∂z = p - y
Sigmoid + BCE loss ∂L/∂z = p - y
MSE for linear model Xw ∇_w = (2/n)·Xᵀ(Xw - y)
aᵀx a
xᵀAx (A + Aᵀ)x (= 2Ax if A symmetric)
‖x‖² 2x

Shapes and costs

Decompositions

Read more: Math for ML · Dimensionality Reduction


Optimization

Method Update rule (g = ∇L(θ)) Notes
GD / SGD θ ← θ - η·g SGD uses a mini-batch estimate of g
Momentum v ← μ·v + g; θ ← θ - η·v μ ≈ 0.9; damps oscillation, speeds up ravines
Nesterov gradient evaluated at look-ahead θ - η·μ·v Slightly better convergence than plain momentum
AdaGrad s ← s + g²; θ ← θ - η·g / (√s + ε) Per-parameter LR; LR decays forever (sparse features)
RMSProp s ← ρ·s + (1-ρ)·g²; θ ← θ - η·g / (√s + ε) Fixes AdaGrad's decay; ρ ≈ 0.9
Adam m ← β₁m + (1-β₁)g; v ← β₂v + (1-β₂)g²; m̂ = m/(1-β₁ᵗ); v̂ = v/(1-β₂ᵗ); θ ← θ - η·m̂/(√v̂ + ε) Defaults β₁=0.9, β₂=0.999, ε=1e-8; bias correction matters early
AdamW Adam step, plus θ ← θ - η·λ·θ applied separately Decoupled weight decay; default for transformers

Weight decay vs L2

LR schedule Formula / shape Used for
Step decay multiply by γ (e.g. 0.1) every s epochs Classic CNNs
Exponential η_t = η_0·γᵗ Simple decay
Cosine η_t = η_min + ½(η_max - η_min)(1 + cos(π·t/T)) Default for modern training
Linear warmup η_t = η_max·t/T_warm for first steps Transformers, large batches (Adam's v is noisy early)
Inverse sqrt η_t ∝ d_model^{-0.5}·min(t^{-0.5}, t·T_warm^{-1.5}) Original transformer
One-cycle ramp up then anneal down Fast convergence on CNNs
Reduce-on-plateau cut LR when val metric stalls Small projects, fine-tuning

Read more: Neural Network Training · Math for ML


Algorithm Cheat Table

n = samples, d = features, k = clusters/neighbours, T = trees, L = tree depth, n_sv = support vectors.

Algorithm Type Train / predict (per sample) Key hyperparams Scale features? Missing values? Pros / cons
Linear regression (OLS/ridge/lasso) Supervised, regression O(nd² + d³) closed form, O(nd) per SGD epoch / O(d) α (L1/L2 strength) Yes if regularised No + fast, interpretable / − linear only, sensitive to outliers and collinearity
Logistic regression Supervised, classification O(nd · iters) / O(d) C (=1/λ), penalty Yes No + calibrated-ish probabilities, strong baseline / − linear boundary
Naive Bayes Supervised, classification O(nd) / O(dK) smoothing α, variant No Can skip features in principle; not in sklearn + tiny data, text / − independence assumption, poor probabilities
k-NN Supervised, lazy O(1) (store) or O(nd log n) tree / O(nd) brute k, distance, weights Yes No + no training, flexible / − slow predict, curse of dimensionality
SVM (kernel) Supervised O(n²) to O(n³) / O(n_sv·d) C, kernel, γ Yes No + strong on small/medium, high-dim data / − doesn't scale past ~100k rows, no native probabilities
Decision tree Supervised O(nd log n) / O(L) max_depth, min_samples_leaf No Impl-dependent (sklearn ≥ 1.3 yes) + interpretable, nonlinear, mixed types / − high variance
Random forest Ensemble (bagging) O(T·n·d' log n), d' = features tried per split / O(T·L) n_estimators, max_features, depth No Impl-dependent (sklearn ≥ 1.4 yes) + robust, little tuning, OOB estimate / − large models, weak extrapolation
Gradient boosting (XGBoost/LightGBM/CatBoost) Ensemble (boosting) O(T·n·d) with histograms / O(T·L) learning_rate, n_estimators, max_depth/num_leaves, subsample No Yes (learned default direction) + best on tabular data / − sequential training, overfits without early stopping
k-means Unsupervised, clustering O(n·k·d·iters) / O(kd) k, init (k-means++) Yes No + simple, fast / − spherical equal-size clusters, needs k, local optima
DBSCAN Unsupervised, clustering O(n log n) with index, O(n²) worst / no native predict eps, min_samples Yes No + arbitrary shapes, finds noise / − struggles with varying density, high d
Gaussian mixture (EM) Unsupervised, density O(n·k·d²·iters) full cov / O(k·d²) k, covariance type Yes No + soft assignments, elliptical clusters / − local optima, sensitive to init
PCA Unsupervised, dim. reduction O(nd² + d³) full, O(ndk) randomised / O(dk) n_components Yes (standardise) No + decorrelates, denoises / − linear, components hard to interpret

Read more: Classical ML Overview · Ensemble Methods · Clustering · Dimensionality Reduction


Bias-Variance Diagnostics

Expected test error = bias² + variance + irreducible noise

Symptom Diagnosis Fixes
Train error high, val error ≈ train error High bias (underfitting) Bigger model, more/better features, less regularisation, train longer, boosting
Train error low, val error much higher High variance (overfitting) More data, augmentation, regularisation (L1/L2, dropout), simpler model, early stopping, bagging
Both high, big gap Both Fix bias first, then variance
Val good, test/prod bad Leakage or distribution shift Audit splits and features; check drift, training-serving skew
Val error noisy across folds Small or heterogeneous val set More folds, repeated CV, stratify, report CI

Learning curves (error vs training-set size)

Knobs and their direction

Knob ↑ increases
Tree depth, num_leaves, polynomial degree, hidden units Variance
k in k-NN Bias
Regularisation λ (or ↓ C in SVM/logreg) Bias
Number of trees in random forest Neither much (variance ↓, plateaus)
Boosting rounds (without early stopping) Variance

Read more: Model Evaluation · Neural Network Training · Ensemble Methods


Neural Network Essentials

Activation Formula Range Notes
Sigmoid 1/(1 + e^{-x}) (0, 1) Output for binary / multi-label; saturates, gradient ≤ 0.25
Tanh (eˣ - e^{-x})/(eˣ + e^{-x}) (-1, 1) Zero-centred; RNN gates/state; still saturates
ReLU max(0, x) [0, ∞) Default for CNNs/MLPs; "dying ReLU"
Leaky ReLU max(αx, x), α ≈ 0.01 (-∞, ∞) Fixes dying ReLU
GELU x·Φ(x) ≈ (-0.17, ∞) BERT, GPT
SiLU / Swish x·σ(x) ≈ (-0.28, ∞) SwiGLU FFN in LLaMA-family models
Softmax e^{z_i}/Σ_j e^{z_j} simplex Multiclass output, attention weights
Init Weight variance Pair with
Xavier / Glorot Var(W) = 2/(fan_in + fan_out) (uniform bound √(6/(fan_in + fan_out))) tanh, sigmoid
He / Kaiming Var(W) = 2/fan_in ReLU family
LeCun Var(W) = 1/fan_in SELU, linear
Normalization Normalizes over Where
BatchNorm Batch (+ spatial) dims, per channel CNNs; running stats at eval; breaks with tiny batches
LayerNorm Features of each sample Transformers, RNNs; same at train and eval
RMSNorm Features, divides by RMS only (no mean subtraction) LLaMA-style LLMs; cheaper than LayerNorm
GroupNorm Groups of channels per sample Detection/segmentation with small batches
InstanceNorm Each channel of each sample Style transfer

Parameter counts

Layer Params Notes
Linear d_in → d_out d_in·d_out + d_out Drop + d_out if no bias
Conv2d (K_h·K_w·C_in + 1)·C_out groups = 1; FLOPs ≈ 2·K_h·K_w·C_in·C_out·H_out·W_out
Conv output size floor((H + 2P - K)/S) + 1 P padding, S stride
Embedding V·d Often tied with output layer
Multi-head attention 4·d² + 4d Q, K, V, O projections; independent of head count
Transformer block (FFN 4×) ≈ 12·d² 4d² attention + 8d² FFN, ignoring biases/norms
Decoder-only LLM N ≈ 12·L·d² + V·d L layers; e.g. L=32, d=4096 → ≈ 6.4B + embeddings
LSTM layer 4·(h·(h + x) + h) Four gates; PyTorch keeps two bias vectors: 4·(h·(h + x) + 2h)

Attention

Attention(Q, K, V) = softmax(Q·Kᵀ / √d_k) · V

Compute rules of thumb

Read more: Neural Network Training · Transformers · Computer Vision · Sequence Models


LLM Numbers

Memory per parameter

dtype Bytes/param 7B model weights
fp32 4 28 GB
fp16 / bf16 2 14 GB
fp8 / int8 1 7 GB
int4 (e.g. GPTQ, AWQ, NF4) 0.5 3.5 GB (+ small overhead for scales)

KV cache

KV bytes = 2 (K and V) × n_layers × n_kv_heads × d_head × seq_len × batch × bytes_per_elem

Rules of thumb (approximate)

Read more: LLM Fundamentals · LLM Inference Optimization · Model Compression · Fine-Tuning


System Design Numbers

All values are approximate orders of magnitude; hardware varies. Use them for back-of-envelope estimates, not SLAs.

Operation Approx. latency
L1 cache reference ~1 ns
Branch mispredict ~5 ns
Main memory reference ~100 ns
Compress 1 KB (fast codec) ~2-3 µs
Read 1 MB sequentially from RAM ~10-100 µs
SSD (NVMe) random read ~10-100 µs
Round trip within a datacenter ~0.5 ms
Read 1 MB sequentially from SSD ~0.1-1 ms
Cache / KV-store lookup over network (Redis) ~0.5-1 ms
HDD seek ~10 ms
Read 1 MB sequentially from HDD ~5-20 ms
Cross-continent round trip ~100-150 ms
Useful quantity Approx. value
Seconds per day 86,400 ≈ 10⁵
Seconds per year ≈ 3.15 × 10⁷
QPS from daily volume requests/day ÷ 86,400; plan peak at 2-3× average
1M float32 vectors × 768 dims ≈ 3 GB (10⁶ · 768 · 4 bytes)
Interactive latency budget Users notice > ~100 ms; recsys/ads end-to-end often 50-200 ms
Online feature-store read (p99) Single-digit to ~10 ms
GBDT inference, one row on CPU ~µs to ~1 ms (depends on trees/depth)
GPU HBM bandwidth (A100 / H100) ~2 / ~3.35 TB/s
Dense bf16 throughput (A100 / H100 SXM) ~312 / ~990 TFLOPS
PCIe 4.0 x16 ~32 GB/s per direction

Read more: ML System Design Patterns · Model Serving · Backend System Design Guide


Top 20 Traps

  1. Target leakage: a feature that is only known after the label (e.g. "refund issued" to predict fraud). Ask "would I have this at prediction time?"
  2. Fitting preprocessing on all data: scalers, imputers, target encoders or feature selection fit before the split. Put them inside a Pipeline fit per fold.
  3. Random split on time series: trains on the future. Use time-based splits / forward-chaining CV with a gap.
  4. Entity leakage: same user, patient or session in train and test. Use GroupKFold.
  5. Accuracy on imbalanced data: 99% accuracy at 1% prevalence is a constant predictor. Use PR-AUC, recall@precision, F1.
  6. ROC-AUC on rare events: looks great while precision is terrible. Report PR-AUC too.
  7. Oversampling (SMOTE) before splitting: synthetic copies of test points leak into training. Resample only the training fold.
  8. Tuning on the test set: every peek makes it a validation set. Hold out a final test set touched once.
  9. Default 0.5 threshold: choose the threshold from costs or a precision/recall target on validation data.
  10. Uncalibrated probabilities: after class weights, undersampling or boosting, scores are not probabilities. Recalibrate (Platt, isotonic) or correct for the sampling rate.
  11. Training-serving skew: features computed differently offline vs online. Share feature code, log served features.
  12. Duplicates across splits: near-duplicate images/texts, or benchmark contamination in LLM pretraining data. Dedupe before splitting.
  13. Peeking at A/B tests: stopping when p < 0.05 inflates false positives. Fix sample size up front or use sequential tests.
  14. Multiple comparisons: 20 metrics at α = 0.05 gives ~1 false win by chance. Pre-register a primary metric; correct (Holm, BH).
  15. Sample ratio mismatch: a 50/50 test landing 52/48 means broken assignment/logging. Check with a chi-square test before reading results.
  16. Correlation ≠ causation: confounding and Simpson's paradox. Feature importance is not a causal effect.
  17. Missing feature scaling: k-NN, SVM, k-means, PCA and regularised linear models all depend on scale; trees don't.
  18. Forgetting model.eval(): dropout stays on and BatchNorm uses batch stats at inference.
  19. Bad metric for the target: MAPE when y can be ~0, RMSE on heavy-tailed targets, R² compared across datasets.
  20. Feedback loops and selection bias: training only on data your model chose to show or approve (clicks on shown items, approved loans). Log propensities, keep exploration traffic.

Read more: Feature Engineering · Time Series · Model Evaluation · A/B Testing · Model Monitoring · Glossary


View or edit this guide on GitHub