ML fundamentals — a textbook progression
ML-fundamentals rounds at top companies are conversational and probing — interviewers keep asking "why?" until they find your floor, and they lean hard on feature selection and Bayes/overlap reasoning. This page is a course, not a Q&A dump: it walks the theory in the order the books do (Hastie ESL / Bishop PRML) — the learning problem, Bayes error, the bias–variance decomposition, regularization, then models, evaluation, RecSys, and finally probe chains tying the theory back to the interview.
Everything in this round reduces to a few ideas: error decomposes into irreducible (Bayes) + bias² + variance (Part I); you trade bias for variance with capacity and regularization; you pick a model to match the regime and the data (Parts II–III); you measure with the right metric and calibrate (Part IV); and at Reddit you apply it to retrieval + ranking (Part V). The interview's favorite question — two overlapping class distributions — is just Bayes error in disguise (Part VI). Be able to derive, not recite, and survive three "why?"s on any answer.
- Part I · Statistical-learning foundations
- 1. The learning problem: loss, risk, generalization
- 2. Bayes optimal classifier & Bayes error
- 3. The bias–variance decomposition
- 4. Capacity, overfitting & regularization
- 5. MLE, MAP & the Bayesian view
- Part II · Classical models
- 6. Linear / logistic regression & GLMs
- 7. Trees & ensembles (bagging, boosting)
- 8. SVM/kernels, k-NN, clustering, PCA
- Part III · Deep learning
- 9. Backprop, activations, init, norm, optimizers
- Part IV · Evaluation & experimentation
- 10. Metrics: ROC/PR, NDCG, cross-validation
- 11. Calibration
- 12. Statistics & A/B testing
- Part V · RecSys fundamentals
- 13. Retrieval, two-tower, negative sampling
- 14. Ranking models & multi-task
- 15. Position bias & counterfactual eval
- Part VI · The Reddit probe chains (applied)
- 16. The overlap-distributions chain
- 17. Feature selection (probed hard)
- 18. The ML-practitioner notebook (#13)
- 19. Rapid-fire probes
Supervised learning assumes data $(x,y)$ drawn i.i.d. from an unknown joint distribution $P(x,y)$. We pick a hypothesis $f$ from a class $\mathcal{F}$ to minimize the expected risk under a loss $L$:
$$ R(f) = \mathbb{E}_{(x,y)\sim P}\big[L(y, f(x))\big]. $$We never know $P$, so we minimize the empirical risk on $n$ samples (ERM — empirical risk minimization):
$$ \hat R(f) = \frac{1}{n}\sum_{i=1}^{n} L(y_i, f(x_i)). $$The whole game is the generalization gap $R(f) - \hat R(f)$: training error is optimistic, and the gap grows with model capacity and shrinks with data. Everything downstream — regularization, validation, the bias–variance tradeoff — is about controlling that gap.
Loss functions and what they assume
| Task | Loss | Implied noise model |
|---|---|---|
| Regression | Squared error $(y-\hat y)^2$ | Gaussian noise → mean is optimal |
| Regression (robust) | Absolute / Huber | Laplace / heavy tails → median is optimal |
| Classification | Cross-entropy $-\sum_c y_c\log \hat p_c$ | Bernoulli/Categorical → calibrated probabilities |
| Ranking | Pairwise / listwise (BPR, LambdaMART) | Order matters, not absolute score |
The loss is a modeling choice. "Which model?" in the interview always starts here: clarify the task (regression / classification / ranking), the label, and the cost of each error type — that fixes the loss before you pick an architecture.
The lowest error any model can achieve is the Bayes error — the error of the Bayes optimal classifier, which predicts the most probable class given $x$:
$$ f^*(x) = \arg\max_{c} P(y=c \mid x), \qquad R^* = \mathbb{E}_x\Big[\,1 - \max_c P(y=c\mid x)\,\Big]. $$This error is irreducible: it comes from genuine overlap between the classes (the same $x$ can yield different $y$), not from a weak model. No amount of data or capacity removes it — only better features that separate the classes can.
The two-Gaussian case (this is the interview's overlap question)
One feature $x$, two equally-likely classes with $x\mid A \sim \mathcal{N}(\mu_A,\sigma^2)$, $x\mid B \sim \mathcal{N}(\mu_B,\sigma^2)$, $\mu_B>\mu_A$. The Bayes classifier thresholds where the densities cross, $t=\tfrac{\mu_A+\mu_B}{2}$, and the Bayes error is the overlap area:
$$ R^* = \Phi\!\left(-\frac{\Delta}{2\sigma}\right), \qquad \Delta = \mu_B-\mu_A, $$with $\Phi$ the standard normal CDF. The point every interviewer wants: error depends on the separation in units of noise, $\Delta/\sigma$. More overlap (small $\Delta/\sigma$) → higher irreducible error → a single threshold cannot fix it; you need a feature that raises $\Delta/\sigma$. With unequal priors or asymmetric costs, the optimal threshold shifts off the midpoint toward the rarer/cheaper class.
For squared-error regression, fix $x$ with $y = g(x) + \varepsilon$, $\mathbb{E}[\varepsilon]=0$, $\mathrm{Var}(\varepsilon)=\sigma^2$. Let $\hat f$ be trained on a random dataset $D$. The expected test error decomposes exactly:
$$ \mathbb{E}_{D,\varepsilon}\big[(y-\hat f(x))^2\big] = \underbrace{\sigma^2}_{\text{irreducible}} + \underbrace{\big(g(x)-\mathbb{E}_D[\hat f(x)]\big)^2}_{\text{bias}^2} + \underbrace{\mathbb{E}_D\big[(\hat f(x)-\mathbb{E}_D[\hat f(x)])^2\big]}_{\text{variance}}. $$Derive it (the cross-term vanishes)
Let $\bar f=\mathbb{E}_D[\hat f(x)]$. Write $y-\hat f = (y-g) + (g-\bar f) + (\bar f-\hat f)$ and square. $\mathbb{E}[(y-g)^2]=\sigma^2$; $(g-\bar f)^2$ is deterministic = bias²; $\mathbb{E}[(\bar f-\hat f)^2]$ = variance. Cross terms vanish: $\varepsilon\perp\hat f$ so $\mathbb{E}[(y-g)(\cdot)]=0$, and $\mathbb{E}_D[\bar f-\hat f]=0$ by definition of $\bar f$.
- Bias: model class wrong on average (too simple). High for linear-on-nonlinear; low for flexible models.
- Variance: how much $\hat f$ jiggles across training sets. High for flexible/low-data; low for rigid.
- Irreducible $\sigma^2$: the Bayes/label-noise floor from ch2, in regression form.
Capacity moves you along the tradeoff: more flexibility ⇒ bias↓, variance↑; total error is U-shaped. The moves: more data cuts variance (not bias); regularization raises bias to cut variance; ensembling targets one term.
Double descent: past the interpolation threshold, very over-parameterized models can have decreasing test error again — the U-curve is the underparameterized half of a double-descent curve. One sentence; don't over-claim.
Overfitting = low train, high test = high variance (fit noise). Underfitting = high train and test = high bias. Diagnose with a learning curve: a large persistent train/val gap ⇒ variance ⇒ regularize / more data; both curves high and close ⇒ bias ⇒ more capacity/features.
L2 vs L1 — geometry and the Bayesian reading
Ridge (L2) adds $\lambda\lVert w\rVert_2^2$; Lasso (L1) adds $\lambda\lVert w\rVert_1$.
- Geometry: the L1 ball has corners on the axes, so the loss contour first touches at a corner ⇒ exact zeros ⇒ sparsity / feature selection. The L2 ball is smooth ⇒ shrinks but rarely zeros.
- Gradient: L2 penalty gradient $2\lambda w$ ∝ $w$ (gentle shrink); L1's $\lambda\,\mathrm{sign}(w)$ is constant (soft-thresholds small weights to 0).
- As a prior (MAP): L2 = Gaussian prior $w\sim\mathcal{N}(0,\tau^2)$; L1 = Laplace prior. Regularization is a prior (ch5).
Capacity control without changing the model: early stopping limits effective capacity; and $\lambda$ is tuned by cross-validation (ch10), never on the training set.
Maximum likelihood picks parameters making the data most probable; MAP adds a prior and is exactly MLE + a regularizer:
$$ \hat\theta_{\text{MLE}} = \arg\max_\theta \sum_i \log p(x_i\mid\theta), \qquad \hat\theta_{\text{MAP}} = \arg\max_\theta \Big[\sum_i \log p(x_i\mid\theta) + \log p(\theta)\Big]. $$- Gaussian prior $\Rightarrow \log p(\theta)\propto-\lambda\lVert\theta\rVert_2^2 \Rightarrow$ L2 / ridge.
- Laplace prior $\Rightarrow \log p(\theta)\propto-\lambda\lVert\theta\rVert_1 \Rightarrow$ L1 / lasso.
Logistic regression worked example. The Bernoulli likelihood gives cross-entropy; MAP with a Gaussian prior gives L2-regularized logistic regression, with gradient $X^\top(\hat p - y) + 2\lambda w$ — the data term pulls predictions toward labels, the prior shrinks weights. Fully Bayesian inference keeps the whole posterior $p(\theta\mid D)$ (predictive uncertainty); MAP is the point-estimate shortcut.
Start every "which model?" answer with the simplest thing that could work, then justify added complexity by the bias–variance regime.
- Linear regression: $\hat y = w^\top x$, squared loss, closed-form $w=(X^\top X)^{-1}X^\top y$ or GD. High bias if truth is nonlinear; interpretable; great baseline.
- Logistic regression: $\hat p = \sigma(w^\top x)$, cross-entropy loss, convex ⇒ global optimum, fast at scale, naturally calibrated. Why not linear regression for classification? linear reg is unbounded (predicts outside $[0,1]$), penalizes correct-but-confident points, and assumes Gaussian noise on a Bernoulli label.
- GLMs unify these: pick an exponential-family response + a link function $g(\mathbb{E}[y])=w^\top x$. Identity link → linear regression; logit → logistic; log → Poisson regression (counts). Choose the family by the target's distribution (continuous / binary / count).
A single decision tree is low-bias, high-variance and captures interactions automatically — but overfits. Ensembles fix the variance or the bias (the bias–variance lens from ch3):
| Bagging / Random Forest | Boosting / GBM, XGBoost | |
|---|---|---|
| Builds | Many deep trees in parallel on bootstrapped samples (+ feature subsampling) | Shallow trees sequentially, each fit to the gradient of the loss (residual) |
| Attacks | Variance (averaging decorrelated trees) | Bias (additively reducing residual error) |
| Overfit risk | Low; more trees never hurts | Higher; needs learning-rate, depth, subsample, L1/L2 regularization, early stopping |
| Key knobs | n_trees, max_features | learning_rate, n_estimators, max_depth, reg_lambda/alpha, subsample |
SVM maximizes the margin between classes; the kernel trick replaces inner products $x_i^\top x_j$ with $k(x_i,x_j)$ to learn nonlinear boundaries in an implicit high-dim space (RBF → infinite-dim). Powerful on small/medium data; scales poorly (kernel matrix is $O(n^2)$), so logistic regression / GBDTs win at scale.
k-NN: no training; predict by the $k$ nearest neighbors. $k$ is a direct bias–variance knob: small $k$ = low bias/high variance (jagged boundary), large $k$ = high bias/smooth. Curse of dimensionality kills it in high dim (everything is far).
Unsupervised: clustering & dimensionality reduction
- k-means minimizes within-cluster variance via Lloyd's algorithm (assign → recompute centroids). Assumes spherical, equal-size clusters; sensitive to init (use k-means++) and scale.
- Choosing k: the elbow (inertia vs k), the silhouette score (cohesion vs separation), the gap statistic (vs a null reference), or — best — a downstream metric if clusters feed a task. There's no single "right" k; justify by the objective.
- GMM + EM: soft clusters; E-step computes responsibilities, M-step updates params; generalizes k-means to elliptical clusters with uncertainty.
- PCA / SVD: PCA projects onto top eigenvectors of the covariance (max-variance directions); computed via SVD $A=U\Sigma V^\top$. Use for compression/denoising/visualization. Gotcha: PCA is unsupervised — top variance directions may not align with the class boundary, so it can hurt a classifier.
Backprop is the chain rule on the computation graph: a forward pass caches activations, a backward pass propagates $\partial L/\partial \cdot$ from output to input, reusing shared subexpressions (it computes vector–Jacobian products, never the full Jacobian).
Activations & the vanishing gradient
Sigmoid's derivative is $\sigma'(z)=\sigma(z)(1-\sigma(z))\le 0.25$, and it saturates to $\approx 0$ for large $|z|$. Stack many layers and the product of small derivatives drives early-layer gradients to zero — training stalls. ReLU ($\max(0,z)$) has derivative 1 on the positive side, so gradients flow; cost is "dying ReLU" (stuck at 0) → Leaky ReLU / GELU fix it. This is why deep nets needed ReLU + good init + normalization to train at all.
Initialization & normalization
- Xavier/Glorot (var $=1/n_\text{in}$) for tanh/sigmoid; Kaiming/He (var $=2/n_\text{in}$) for ReLU — keep activation/gradient variance stable across depth.
- BatchNorm normalizes per-mini-batch (stabilizes training, allows higher LR, mild regularizer; awkward at small batch / inference). LayerNorm normalizes per-example across features (batch-independent → the default in transformers).
Optimizers & regularization
SGD → +momentum (velocity) → Adam (per-parameter adaptive LR via 1st/2nd moment estimates with bias correction) → AdamW (decoupled weight decay — the correct way to L2-regularize adaptive optimizers). Regularize with weight decay, dropout (randomly zero activations at train time ≈ ensembling thinned subnetworks; scale at test), early stopping, and augmentation. LR schedule (warmup + cosine) matters as much as the optimizer.
From the confusion matrix: precision $=\tfrac{TP}{TP+FP}$ (of what you flagged, how much was right), recall $=\tfrac{TP}{TP+FN}$ (of what's real, how much you caught), F1 = harmonic mean. The threshold trades precision for recall — pick it by the cost of FP vs FN (ties back to ch2's cost-shifted threshold).
| Metric | Use when | Pitfall |
|---|---|---|
| Accuracy | Balanced classes, symmetric costs | Useless under imbalance (99% by predicting majority) |
| ROC-AUC | Ranking quality, threshold-free, balanced-ish | Over-optimistic under heavy imbalance |
| PR-AUC | Rare positives (fraud, spam, click) | Baseline = positive rate, not 0.5 |
| NDCG / MAP / MRR | Ranked lists (feed, search) | Need graded relevance + position discount |
NDCG rewards relevant items near the top via a logarithmic position discount:
$$ \text{DCG@k} = \sum_{i=1}^{k} \frac{2^{\text{rel}_i}-1}{\log_2(i+1)}, \qquad \text{NDCG@k}=\frac{\text{DCG@k}}{\text{IDCG@k}}. $$Cross-validation done right
k-fold averages out variance in the estimate; stratified k-fold preserves class ratios (essential for imbalance); time-series splits (train past → test future) for any temporal data — random splits leak the future. The cardinal sin is leakage: any preprocessing/feature-selection/target-encoding fit on the full data before splitting inflates CV scores. Fit transforms inside the fold.
Discrimination (AUC) asks "can you rank positives above negatives?"; calibration asks "when you say 0.7, does it happen 70% of the time?" A model can rank perfectly yet be badly calibrated. Diagnose with a reliability diagram (predicted vs observed frequency per bin) and ECE (expected calibration error).
- Platt scaling: fit a logistic on the scores (1 param of slope/intercept) — good for small data.
- Isotonic regression: a flexible monotone map — more data-hungry, more powerful.
- Negative downsampling distorts probabilities: recover with $p_\text{true} = \tfrac{p}{p + (1-p)/r}$ for sampling rate $r$.
Reddit is product/experimentation-heavy; expect this even in an ML round.
- p-value = $P(\text{data this extreme}\mid H_0)$ — not $P(H_0\mid\text{data})$. A small p rejects $H_0$; it doesn't measure effect size or the probability the null is true.
- Type I (false positive, rate $\alpha$) vs Type II (false negative, rate $\beta$); power $=1-\beta$. Sample size grows with required power and shrinks with the minimum detectable effect (MDE) and lower variance.
- Multiple testing: many concurrent metrics/variants inflate false positives → Bonferroni (conservative) or Benjamini–Hochberg FDR.
- Novelty & primacy effects: users react to change, not just quality — run long enough (weeks) for the effect to wear off; watch longitudinal curves.
- CUPED: use a pre-experiment covariate (e.g., the user's prior CTR) as a control variate to cut variance → more power at the same sample size.
- Interference / marketplace: feed/notification changes spill across users; use switchback or cluster-randomized designs when SUTVA breaks.
Industrial recsys is a funnel: retrieval/candidate-generation (millions → hundreds, high recall, cheap) → ranking (hundreds → ordered, rich features) → re-ranking (diversity, business rules). The bias–variance/serving tradeoff lives here: retrieval must be sub-linear, ranking can be expensive per item.
Two-tower: a user tower and an item tower produce embeddings; relevance = dot product. Item embeddings are precomputed and indexed for ANN (HNSW / IVF-PQ) so retrieval is milliseconds over billions. (Standard industry retrieval; confirmed at Reddit specifically in the notification retrieval stage.)
Negative sampling — the crux of two-tower training
- In-batch negatives: reuse other positives in the batch as negatives — free and scalable, but popularity-biased (popular items appear as negatives more often, so the model over-penalizes them).
- logQ correction: subtract the sampling log-probability from the logit to debias: $s^c_{ij} = s_{ij} - \log Q(j)$, where $Q(j)$ is item $j$'s in-batch sampling probability. Without it, the model systematically under-ranks popular items at serving.
- Hard negatives: items the current model scores high but aren't positives — strong gradient, faster learning; risk of false negatives (true positives the user just hasn't seen).
- Mixed: in-batch (coverage) + sampled (debias) + a few hard (sharpness) is the production recipe.
- GBDT ranker: strong tabular baseline. DLRM / DCN(-v2): embeddings for sparse IDs + explicit feature-cross layers + MLP — captures high-order interactions at scale.
- Multi-task (predict click, upvote, comment, dwell jointly): MMoE = shared experts + per-task softmax gates; PLE adds task-specific experts alongside shared ones, which beats MMoE when tasks conflict (the "seesaw" effect) by protecting each task's private experts from cross-task gradient interference.
- Sequence models (user history): DIN (target-attention over history) → SIM (two-stage long-history) → HSTU (generative, scaling-law). One-liners are enough unless you're on a ranking team; full depth on the RecSys page.
- Loss: pointwise (per-item BCE, easy + needs calibration), pairwise (BPR/LambdaMART, optimizes order), listwise (optimizes the whole list / NDCG).
- Multi-objective serving score: $\sum_t w_t\,p_t$ with product-tuned weights — only valid if heads are calibrated (ch11). Final re-rank applies diversity (MMR/DPP) + freshness + integrity.
Logged ranking data is biased: top items get clicked because they're on top, not (only) because they're relevant. Naively training on it makes the model mimic the old ranker.
- Examination model: $P(\text{click}) = P(\text{examine}\mid\text{position})\cdot P(\text{relevant})$. Estimate the position propensity (e.g., from a randomized swap experiment), then reweight.
- IPS (inverse propensity scoring): weight each logged event by $1/P(\text{examine})$ → unbiased but high variance when propensities are small. SNIPS self-normalizes to stabilize; doubly-robust combines IPS with a reward model (consistent if either is right).
- Cold start: new user → onboarding signals / popularity / context; new item → content embeddings / creator priors / an exploration budget. Exploration: ε-greedy, UCB, or Thompson sampling to avoid feedback loops.
- Offline ≠ online: AUC up but the A/B loses — usually selection bias (you only logged what the old policy showed), calibration drift, a multi-task head regressing, or engagement-bait. Use counterfactual estimators offline, then A/B (ch12).
This is Reddit's most-reported ML-fundamentals sequence (1Point3Acres + darkinterview). It's a single thread that walks from "what model?" down to a mini design — and it's all the Part-I theory applied. Cross-link: real question #12. The interviewer gives hints and follows your reasoning, so narrate.
- "Given feature $x$ and label $y\in\{A,B\}$, how would you model it?" → Clarify it's binary classification; start with a baseline (logistic regression), state you'd add complexity only if the data demands it. Mention you'd look at the class-conditional distributions of $x$ first.
- They show two overlapping normals. → "The overlap is irreducible Bayes error on this feature (ch2): the same $x$ produces both labels there, so no decision rule drives error to zero." Give $R^*=\Phi(-\Delta/2\sigma)$ and the $\Delta/\sigma$ intuition. Do not stop at "linearly separable, pick a threshold" — that's the failing answer.
- "How do you predict points in the overlap?" → Output a probability, not a hard label; place the threshold by the cost of FP vs FN (and the class prior), not the geometric midpoint. If you must hard-classify, you'll be wrong $R^*$ of the time there — that's expected.
- "How would you reduce the error?" → Not by tuning the threshold — by adding features that separate the classes (raise $\Delta/\sigma$ in the joint space). This is the line they're listening for, and it leads straight into feature selection (ch17).
- "Now $x$ has 1000 features, not 1 — what's the same, what changes, what's new?" → Same: Bayes error, bias–variance, the probabilistic framing. Changes: distances concentrate (curse of dimensionality), variance/overfitting risk rises → need regularization + feature selection; visualization breaks. New: feature interactions and correlations matter; multicollinearity; you can now actually separate classes a single feature couldn't.
- "Train acc 0.91, test 0.85 — what's wrong?" → Overfitting / variance (ch4): regularize, more data, fewer features, early stopping; rule out leakage/shift first.
- "Design a model to predict post popularity." → A mini design: define the label (early-velocity vs final score; classification "will it hit top-K?" vs regression on log-score), pick features (author/subreddit history, early engagement, content embeddings, time-of-day), watch leakage (no post-publication signals for an at-publish prediction), use a temporal split + PR-AUC/NDCG, and handle cold start. Tie back: it's the same loss → model → eval → bias-variance loop.
Reddit interviewers repeatedly steer the fundamentals round to "how do you choose features?" Have a structured answer.
- Filter methods: rank by a univariate stat (correlation, mutual information, chi-squared) before modeling — fast, model-agnostic, misses interactions.
- Wrapper methods: search subsets by model performance (forward/backward selection, RFE) — accurate, expensive, overfit-prone.
- Embedded methods: selection happens during training — L1/Lasso zeros weights (ch4), tree gain/importance, gradient-boosting feature usage. Usually the best default.
- Importance done right: prefer permutation importance or SHAP over impurity gain (which is biased toward high-cardinality features).
- Leakage is the #1 trap: features that encode the label or future information (e.g., target-encoding fit on the full data, post-outcome fields). Select inside the CV fold, never on the whole dataset, or you leak and your CV lies.
- When more features hurt: added variance/overfitting, multicollinearity, serving cost/latency, and training-serving skew if a feature isn't reliably available online. More is not better past the point where marginal signal < added variance.
The senior MLE phone screen is often a live notebook: Post Click Prediction — features are hours spent reading category A/B/C + the current post category; label is click. Cross-link: real question #13. They allow Googling syntax; they care about workflow + reasoning, not memorized APIs.
- Load & inspect: JSON → DataFrame; check dtypes, the label balance, missingness. (Reported data is clean and roughly balanced.)
- Prepare: one-hot the categorical
current_post_category; train/test split withstratify; standardize for linear models. - Baseline → models: DummyClassifier (sanity floor) → Logistic Regression (fast, interpretable) → Random Forest (nonlinear interactions) → XGBoost (stronger). The progression itself is the signal.
- Metric choice: justify by the product goal — accuracy if balanced + symmetric cost; ROC-AUC for ranking quality; PR-AUC if positives are rare; F1 for a precision/recall balance.
- "With more time?": cross-validation, hyperparameter tuning, threshold tuning, interaction features (reading-profile × current-category), calibration, error analysis by category.
Be ready for the follow-ups: "why this model / why not that one / what's the tradeoff?" — answer through the bias–variance lens (ch3) and the data size/shape.
Short Q&A to drill recall — grouped by the part they test, so they reinforce the progression.