Skip to content
Tech Interview Prep
Technical interview guide

Regularization (L1/L2, Dropout)

Penalizing model complexity to fight overfitting: L1/L2 weight penalties and dropout.

Minutes to read
4
Practice MCQs
25
Interview Q&A
25
Edition
v5
Editorial status
Reviewed

Scope: scikit-learn stable linear-model and cross-validation guidance and Google ML Crash Course overfitting guidance accessed 2026-08-31..

Overview

Curated: · Written: · Reviewed:

Regularization (L1/L2, Dropout)

In one line

Regularization adds a complexity penalty or a capacity limit to training so the fitted model generalizes better. Reach for it when a flexible model overfits, when features are numerous or correlated, or when the training error is low but validation error is not.

Key definitions

  • Penalty strength (λ): the multiplier on the complexity term. In scikit-learn LogisticRegression, C is inverse strength, so a larger C means a weaker penalty.
  • L2 penalty: the sum of squared coefficients. It shrinks coefficients smoothly and rarely makes them exactly zero.
  • L1 penalty: the sum of absolute coefficients. It can set coefficients exactly to zero.
  • Elastic net: a mix of L1 and L2, with total strength and mixing ratio (l1_ratio) both tuned.
  • Dropout: randomly masks hidden activations during training. Inference is deterministic and uses no mask.
  • Early stopping: halting training at the validation checkpoint chosen by a declared metric, patience and maximum budget.

Formulas and complexity

J(w)=∑iℓ(yi,w⊤xi)+λ∥w∥22J(w)=\sum_i \ell(y_i, w^\top x_i)+\lambda\lVert w\rVert_2^2 J(w)=∑iℓ(yi,w⊤xi)+λ∥w∥1J(w)=\sum_i \ell(y_i, w^\top x_i)+\lambda\lVert w\rVert_1

One feature, no intercept, one observation $(x,y)=(1,3)$: L2 gives $\beta = 3/(1+\lambda)$ and L1 gives $\beta=\max(3-\lambda/2,,0)$. L1 reaches exactly zero at $\lambda=6$, while L2 at $\lambda=8$ is still $0.333$.

For gradient descent with the L2 term, each update is $w \leftarrow w - \eta(g + \lambda w)$, where $g$ is the data-loss gradient. With inverted dropout and drop probability $p$, survivors are scaled by $1/(1-p)$ during training so the expected activation matches inference.

How it works

The optimizer minimizes data loss plus a penalty on coefficient size, so each coefficient is pulled toward zero by a force set by λ. L2 pulls all coefficients proportionally and shares weight across correlated predictors. L1 applies a constant pull, so small coefficients hit zero and drop out. Dropout instead trains a random subnetwork on each step, which discourages units from relying on each other. Early stopping caps how far the optimizer moves from initialization.

Data loss on training rows Add lambda times penalty Optimizer minimizes total objective Smaller or zero coefficients Score on validation folds Choose lambda, then test once

When to use and when not to

SituationPreferBe wary of
Many correlated predictors, prediction is the goalL2L1 alone, since selection among near-duplicates is arbitrary
Many features, most believed irrelevant, sparse model neededL1 or elastic netReading a nonzero coefficient as causal importance
Correlated groups plus sparsity wantedElastic netChoosing the mix ratio without CV
Deep network overfitsDropout, weight decay, early stoppingStacking every regularizer at default strength
Goal is an effect estimate with uncertaintyUnpenalized model with controlsRegularized standard errors or p-values
Training error high and validation also poorLess regularizationMore penalty

Traps interviewers probe

  1. Unscaled features. The penalty tracks units, so a feature in small units gets a larger coefficient and is penalized more. Fix: standardize, and fit the scaler inside each CV fold.
  2. Reading L1 selection as causal. Correlated predictors enter or leave across resamples. Fix: check selected features across bootstraps or folds and say the selection is predictive, not causal.
  3. Leaking the test set. Tuning λ on test data or fitting the scaler on all rows makes the score optimistic. Fix: tune inside a pipeline with time or group-aware CV, then evaluate the locked model once.
  4. Calling a noisy early-stopping curve regularization. A bad learning rate or nonconvergence can look like it. Fix: declare the metric, patience and budget, and confirm the optimizer converged.
  5. Reporting p-values for penalized coefficients. Regularized estimates are biased and the ordinary standard errors do not apply. Fix: use an unpenalized specification for inference.

Interview tips

Open by naming the penalty and what it changes: "L2 shrinks coefficients smoothly, L1 can zero them, and both trade variance for bias through λ." Then state the assumption that matters, usually scale.

Follow-ups to expect: why standardization is required, what C means in logistic regression, how to pick λ without leakage, how elastic net handles correlated groups, and why dropout is disabled at serving.

30-second outline:

  1. Objective: data loss plus λ times a penalty on coefficients.
  2. L2 for correlated predictors, L1 for sparsity, elastic net for both.
  3. Penalty depends on scale, so standardize inside the fold.
  4. Tune λ with CV on a pipeline, then score the test set once.
  5. Diagnose with train and validation curves: high training error with poor validation means too much penalty.

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.