Skip to content
advanced Phase 18 · Production Capstone and ML Interview Preparation

ML Interview Question Patterns

Practice derivations, metric choices, and project narratives interviewers probe.

1h 15m
0 problems
Topic Progress 0%

Whiteboard Derivations: The Gradient Step, the Bias-Variance Split, and "Which Features Matter?"

ML interviews recycle a small set of question families. Learn the families and each new prompt becomes a variant instead of a surprise. The three you meet in almost every loop: a derivation you perform live, a diagnostic you explain, and an interpretation you defend.

Family Typical prompt What is actually being scored
Derivation "Derive the gradient-descent update for linear regression" notation discipline and chain-rule control
Diagnostic "Train error is high and validation error is high. Why?" mapping symptoms to bias/variance causes
Interpretation "Which features drive your model's predictions?" method choice plus honest caveats

For the derivation, the failure mode is starting to scribble. State the loss before you differentiate it — the derivation is graded on structure, not speed. Work it as: define notation, write the objective, differentiate term by term, simplify, state the update.

With ŷ = wᵀx and mean squared error:

L(w) = (1/N) Σ (y_i - w_iᵀx_i)²
∂L/∂w = (1/N) Σ 2(y_i - wᵀx_i) · (-(x_i))   # chain rule on the residual
      = -(2/N) Σ x_i (y_i - wᵀx_i)           # the classic normal-equations form
update:  w ← w - η · ∇L(w) = w + (2η/N) Σ x_i (y_i - wᵀx_i)

Say aloud the three variants afterward — stochastic (one sample), mini-batch (the practical default), and the learning rate's role in trading convergence against stability — because that is where the follow-up questions live. If they ask for code, ten lines is enough:

def gradient_step(X, y, w, eta):
    grad = -(2 / len(y)) * X.T @ (y - X @ w)   # one line, same math as the board
    return w - eta * grad

Two follow-ups are near-guaranteed, so pre-load them. "What if the loss oscillates instead of converging?" — the step is too large for the local curvature: lower the learning rate or add momentum, and note that mini-batch noise acts like a regularization term that fades as the batch grows. "Why does feature scaling matter here?" — because each coordinate's gradient magnitude is proportional to that feature's units, so unscaled inputs give effectively different step sizes per coordinate; the error contours stretch into narrow ravines and a naive step bounces across them instead of descending. Both answers are corollaries of the gradient you just derived, which is exactly why the derivation is worth performing slowly rather than recalling as a memorized formula.

The diagnostic family keys off the decomposition E[(y - ŷ)²] = bias² + variance + noise. Map symptoms to terms instead of reciting definitions:

Train error Validation error Diagnosis Lever
High High high bias (underfit) capacity, better features, longer training
Low High high variance (overfit) more data, regularization, early stopping
Low Low done — or check for leakage verify the split is honest

Interviewers notice candidates who claim "low train error means the model is great" without naming variance; the follow-up is almost always "so what would you change first?" — answer with the lever, and reference the systematic treatment in Diagnosing the Generalization Gap.

The interpretation family — "which features matter?" — is won by separating three things: importance is method-dependent, correlation splits credit, and importance is not causation. A defensible answer: "I'd compute permutation importance on the time-based holdout — shuffle the column, measure the metric drop — then check direction with partial dependence and confirm the ranking is stable across folds. Coefficients only rank features if inputs were standardized first; SHAP gives per-prediction attributions when I need to explain an individual case." Name the caveat before they do; that is the whole point of the question.

The 0.3% Fraud Question: Choosing Metrics When Accuracy Is Guaranteed to Lie

"Fraud is 0.3% of transactions. What metric do you optimize?" This is the metric-choice family, and it is asked as often as derivations because the wrong answer reveals someone who optimizes numbers instead of decisions.

Start by doing the arithmetic out loud. One million transactions a day, 3,000 genuinely fraudulent. Your model flags the top 4,000 for review and catches 2,400 of them:

Actually fraud Actually legit Total
Flagged 2,400 (TP) 1,600 (FP) 4,000
Cleared 600 (FN) 995,400 (TN) 996,000

Precision = 2,400 / 4,000 = 60%. Recall = 2,400 / 3,000 = 80%. Accuracy = 99.78% — while the "flag nothing" classifier scores 99.70%. With a 0.3% base rate, accuracy measures nothing; precision at your review capacity is the number that pays, and the 0.08-point accuracy gap between a useless model and a strong one is exactly why interviewers use this example.

Build the metric answer from the constraint, not from habit:

Scenario Primary metric Why this one
Fraud with a fixed review queue precision@4,000, plus PR-AUC capacity caps flags; ROC-AUC hides FP cost under imbalance
Feed or search ranking NDCG@10 position of relevant items is the product
Risk score consumed by rules log-loss or Brier score downstream thresholds need calibrated probabilities
Miss cost ≫ review cost recall at a fixed false-positive rate bounds the harm while spending a bounded budget

Why not ROC-AUC, the default answer many candidates give? It plots true against false positive rate across every threshold, and with 99.7% negatives the false-positive denominator is enormous — a model can look superb on that curve while still burying the review team in false alarms. Precision-recall trades in the numbers operations actually feels: of the flags, how many were real; of the fraud, how much got through. Saying that asymmetry out loud — "ROC-AUC hides false-positive cost under imbalance" — is the sentence this question is fishing for.

Then connect metric to threshold, which is where the strongest candidates separate themselves. Flag a transaction when the expected cost of clearing exceeds the cost of reviewing: with a $15 review and a $500 average loss, predict fraud whenever p > C_FP / (C_FP + C_FN) = 15 / 515 ≈ 0.029. That single line requires calibrated probabilities — which loops back to log-loss — and it shows you treat the metric as a decision rule with a cost model behind it, not a scoreboard.

Interviewers also probe the imbalance toolkit, so have the order ready: move the threshold first (no retraining, no new risk), then class weights or focal loss if the model never learns the minority class, then resampling — with two warnings attached. Oversampling the minority duplicates examples and distorts the probabilities you ship, so recalibrate afterward (isotonic or Platt scaling on a clean validation fold) before any cost-based threshold trusts them. And rebalance only the training fold: resample the test set as well and your precision describes a prevalence the production system will never see. Reach for anomaly detection only when labels are genuinely scarce, not because the classes are unequal. Close by restating the pipeline: base rate → constraint → metric → threshold → the trade-off you are deliberately accepting.

Leakage, Reproducibility, and the Three-Minute Failure Story

Two families remain, and both are about judgment rather than math: catching leakage, and narrating your own failure. Interviewers probe failures to watch how you localize cause — not to hear that you worked hard.

Leakage questions usually arrive disguised as a success story: "My model got 0.98 AUC — oh, and one feature is days_to_default, computed from the customer's payment history." Learn the four flavors and their signatures:

Leakage type Example Signature Fix
Target leakage feature derived from the outcome suspiciously dominant importance recompute as-of prediction time
Temporal random split on time-ordered data score collapses on a time split hold out the last N weeks
Split/entity same user in train and test group split drops the metric split by entity or session
Preprocessing scaler or selector fit on all data mild, insidious inflation fit inside the CV pipeline only

The detection heuristic to say in the room: when a feature's importance is implausibly high or a score implausibly good, audit its construction timeline before celebrating. A quick ablation — drop the suspect column, retrain — converts suspicion into evidence in one run.

The reproducibility question ("how would someone else reproduce your numbers?") is a four-part answer: freeze and hash the dataset, pin dependencies in a lockfile, fix seeds and record the config with the git SHA, and report a tolerance — a mean over three seeds rather than one figure. That answer is deliberately identical to what a portfolio must demonstrate; Portfolio and Reproducibility turns it into artifacts a reviewer can execute. Two follow-ups are worth having ready: "what if someone cannot get your data?" — publish the fetch script with license notes, and where redistribution is barred, ship a synthetic generator plus the checksum of the original so their pipeline still runs end to end; "how do you know the numbers hold next quarter?" — you do not, so you re-run evaluation on a refreshed snapshot on a schedule and treat any move outside the declared tolerance as a bug report against either the data or the model, whichever the drift monitor implicates.

The failure narrative is the question candidates rehearse least, and it costs them the most. Structure it in three minutes with fixed beats:

  1. Context — two sentences: what the model decided, and the baseline it had to beat.
  2. The claim — what you shipped and the metric that looked good (0.93 AUC).
  3. The crack — who noticed, and what measurement exposed it (a retrain on fresh data scored 0.78).
  4. Root cause — one specific mechanism (a feature joined against post-outcome data).
  5. The fix and the delta — recomputed as-of, retrained, landed at 0.81, still +0.09 over baseline, shipped with a drift alarm on the repaired feature.

The tempting version — "it was hard, I tried many things, eventually it worked" — answers nothing. The scored version names the instrument that revealed the failure, the mechanism, and what you changed in your process afterward: now every feature gets a construction-timeline review before it enters training. Practice it aloud against a stopwatch once: three minutes is roughly 450 spoken words, which enforces the discipline of one cause and one fix instead of a tour of every experiment you ran. If the interviewer interrupts to ask what the metric was, you are in the part of the answer they care about — keep the numbers ready and the chronology intact. That last beat is the one they remember, because it shows the failure produced a system, not just a lesson.

Quiz

1. For L(w) = (1/N) Σ (y_i - wᵀx_i)², what is the gradient with respect to w?

Question 1 options

2. Fraud is 0.3% of 1M daily transactions and your review team can inspect 4,000 a day. Which metric do you optimize?

Question 2 options

3. A credit model reports 0.98 AUC using a feature called days_to_default, computed from the customer's full payment history. What is wrong?

Question 3 options

4. You rank feature importance by the coefficients of a logistic regression trained on raw, unstandardized inputs. What is the core flaw?

Question 4 options

Flashcards

Question

How do you structure a live derivation of the gradient-descent update?

Answer

Define notation, state the loss explicitly, apply the chain rule term by term, simplify to the closed-form gradient, then write the update `w ← w - η·∇L(w)`. Finish with the variants — stochastic versus mini-batch — and the learning rate's role, because the follow-ups live there. Never start scribbling before the objective is written down.

Question

What do the train/validation error combinations diagnose, and which lever follows?

Answer

High train + high validation → high bias, fix with capacity, features, or training time. Low train + high validation → high variance, fix with more data, regularization, or early stopping. Low + low → verify the split for leakage before declaring success. Always name the lever, not just the diagnosis.

Question

Which metric and threshold rule do you give for 0.3% base-rate fraud with a 4,000/day review queue?

Answer

Precision at 4,000 as the primary metric (PR-AUC as the threshold-free summary), because capacity caps flags and accuracy is dominated by negatives. Threshold from costs: flag when `p > C_FP / (C_FP + C_FN)` — with $15 review and $500 loss that is ~0.029 — which presupposes calibrated probabilities (log-loss).

Question

Name the four leakage families and the fastest detection move for each.

Answer

Target leakage (feature encodes the outcome — check its construction timeline, ablate it), temporal leakage (random split on time-ordered data — re-evaluate on a time split), split/entity leakage (same user in train and test — group split), and preprocessing leakage (scaler or selector fit on all data — move it inside the CV pipeline).

Question

What beats a three-minute failure narrative?

Answer

Context and claim in two sentences, the measurement that exposed the crack, one specific root-cause mechanism, the fix with before/after numbers, and the process change you shipped afterward. The final beat — the rule you now follow — is what interviewers remember, because it converts a failure into a system.

Revision Notes

Key Takeaways

  • 1. Question families repeat: derivation, diagnostic, interpretation, metric choice, leakage, failure narrative — recognize the family and the prompt becomes a variant
  • 2. Write the loss before differentiating; derivations are graded on notation discipline and the chain rule, not on speed
  • 3. With a 0.3% base rate, accuracy is meaningless — precision at your operational capacity, plus a cost-derived threshold, is the decision-relevant answer
  • 4. Importance is method-dependent, correlated features split credit, and importance never implies causation — say the caveat before the interviewer does
  • 5. Failure stories score on causal localization: the instrument that exposed the problem, the mechanism, the before/after numbers, and the process you changed

Interview Tips

  • • "Derive the gradient update" — say "loss first," write `L(w)`, differentiate step by step, and narrate the chain rule; candidates who jump to the answer get interrupted with "show the derivative of the residual"
  • • "What metric would you use for X?" — answer in four moves: base rate, hard constraint (capacity or cost), primary metric, threshold rule with a cost formula; naming the constraint is what earns the point
  • • "Your AUC is suspiciously high — what do you check?" — go straight to feature construction timelines and the split clock, propose dropping the suspect column and retraining as the test; mention the four leakage families by name
  • • "Tell me about a model that failed" — use the fixed beats (claim → crack → mechanism → fix → delta → process change) and end with the rule you now enforce; never spend the answer on effort rather than causality

Cheat Sheet

ML Interview Question Families

Derivation (scored on structure): notation → loss → chain rule → simplify → update → variants.

L(w) = (1/N) Σ (y - wᵀx)²
∇L   = -(2/N) Σ x(y - wᵀx)
w    ← w - η·∇L

Diagnostics: train high/val high = bias → capacity & features · train low/val high = variance → data & regularization · both low → check leakage.

Metric choice:

Constraint Metric
Fixed review/flag capacity precision@k (+ PR-AUC)
Ranked positions (feed/search) NDCG@10
Scores consumed by rules log-loss / Brier (calibration)
Miss ≫ review cost recall at fixed FPR

Threshold from costs: flag when p > C_FP / (C_FP + C_FN) — requires calibrated probabilities.

Leakage table: target (recompute as-of) · temporal (time-based split) · split/entity (group split) · preprocessing (fit inside CV).

Failure narrative beats: context → claim → crack → mechanism → fix + delta → process change.

Reproducibility answer: hashed data snapshot · lockfile pins · seeds + config + git SHA · reported tolerance over ≥3 seeds.

Continue Learning