Classical Machine LearningIntermediate19 min read5 questions

Linear & Logistic Regression Deep Dive

Interviewers still open with linear models because they expose whether you understand optimisation, probability and diagnostics — or just call .fit().

Covers: Linear regression assumptions, logistic regression & odds, L1/L2 regularization, gradient descent variants, multicollinearity

Linear and logistic regression remain the most-asked models in ML interviews, at every level. Not because companies deploy them everywhere (many do), but because they are the smallest models where you can be asked about the loss function, the optimiser, the probabilistic interpretation, regularisation and diagnostics — all in one question.

Filter
0/5 mastered
Intermediatelinear-regressiondiagnosticsassumptionsAsked at Capital One, quant/analytics loops

30-second answer

Linearity in the parameters, independent errors, homoscedasticity, normally distributed errors, and no perfect multicollinearity. In practice linearity and independence matter most; normality only matters for small-sample inference (the CLT rescues large samples), and heteroscedasticity biases your standard errors rather than your coefficients — fixable with robust standard errors.

Advancedlogistic-regressioncross-entropyoptimizationAsked at Google, Meta

30-second answer

MSE combined with a sigmoid is non-convex in the weights, so gradient descent can stall in local minima, and its gradient contains a σ'(z) factor that vanishes exactly when the model is confidently wrong. Cross-entropy is convex for logistic regression and its gradient simplifies beautifully to (p − y)x — the sigmoid derivative cancels, so confident mistakes produce large gradients.

IntermediateregularizationlassoridgeAsked at Capital One, Two Sigma

30-second answer

L2 (ridge) shrinks all coefficients smoothly, handles correlated features by splitting weight between them, and never zeroes anything. L1 (lasso) produces exact zeros for automatic feature selection but arbitrarily picks one from a correlated group. Elastic net combines both to get sparsity plus stable grouping. Tune λ by cross-validation over a log-spaced grid, always on standardised features.

Intermediateoptimizationgradient-descenttrainingAsked at NVIDIA, Meta AI

30-second answer

Batch GD uses the whole dataset per step: an exact gradient but one update per epoch and no ability to scale past memory. SGD uses one sample: very noisy but many fast updates. Mini-batch takes 32–1024 samples, which gives a low-variance gradient estimate, saturates GPU parallelism, and keeps enough noise to escape sharp minima. It is the only one of the three that maps well onto hardware.

Advancedfeature-engineeringencodinghigh-cardinalityAsked at Booking.com, Instacart

30-second answer

One-hot is out — 50k sparse columns. The realistic options are target/mean encoding with out-of-fold computation and smoothing, hashing to a fixed number of buckets, learned embeddings if you have a neural model, frequency encoding, or grouping the long tail into an 'other' bucket. The choice depends on model family, cardinality of the *useful* signal, and whether new categories appear at serving time.

Showing 5 of 5 questions for regression-and-regularization-interview.

Check your understanding

5 questions · no sign-up, nothing stored

0/5 answered
Question 1

1.Heteroscedastic residuals in OLS primarily invalidate:

Question 2

2.The gradient of binary cross-entropy with respect to the pre-activation z is:

Question 3

3.Two features are correlated at 0.98. Lasso will most likely:

Question 4

4.You increase batch size from 128 to 512. According to the linear scaling rule you should:

Question 5

5.Target encoding computed once on the full training set before cross-validation:

Hands-on challenge

Build it — this is what you talk about in a deep-dive round.

Implement regularised logistic regression from scratch

Write a NumPy implementation and validate it against scikit-learn, then use it to demonstrate the MSE failure mode.

Requirements

  • Numerically stable sigmoid and log-loss (no `log(0)`, use the log-sum-exp trick).
  • Full-batch and mini-batch gradient descent with configurable L1/L2/elastic-net penalties, intercept unpenalised.
  • Match `sklearn.linear_model.LogisticRegression` coefficients to 1e-3 on the same objective.
  • Reproduce the confidently-wrong gradient comparison between cross-entropy and MSE, and plot it.

Stretch goals

  • Add a Newton/IRLS solver and compare its iteration count to gradient descent.
  • Implement out-of-fold smoothed target encoding as a scikit-learn transformer and prove it is leak-free.