Implement L2-Regularized Logistic Regression With Gradient Descent

Last updated: November 29, 2026

Quick Overview

Implement logistic regression with an L2 penalty using batch gradient descent, without ML libraries, for predicting whether a customer will click a Subscribe and Save offer. Show how the penalty changes the learned weights on separable data.

Amazon
Machine Learning
Machine Learning Engineer
Amazon
November 29, 2026
Machine Learning Engineer
Technical Screen
Machine Learning
Medium

0

0

0 solved


Implement logistic regression with an L2 penalty using batch gradient descent, without ML libraries, for predicting whether a customer will click a Subscribe and Save offer. Show how the penalty changes the learned weights on separable data.

Amazon MLE screens sometimes ask candidates to write a model from scratch to confirm they understand the math under the libraries. The interviewer checks the gradient, numerical stability, not penalizing the bias, and an explanation of why regularization matters when data is separable.

What the Interviewer Expects
  • Derives the gradient of log loss plus the L2 term
  • Implements a numerically stable sigmoid
  • Excludes the intercept from the penalty
  • Shows that unregularized weights grow on separable data
  • Discusses learning rate, convergence and feature scaling
Key Topics to Cover
logistic regression
gradient descent
L2 regularization
numerical stability
separable data
How to Approach This
  1. Understand the bias-variance trade-off. High training accuracy but low test accuracy signals overfitting.
  2. Choose evaluation metrics carefully based on the problem. Accuracy alone is often insufficient.
  3. Feature engineering is often more impactful than model selection.
  4. Know when to use tree-based models (tabular data) vs neural networks (unstructured data).
  5. Handle class imbalance with SMOTE, class weights, or appropriate loss functions.
Possible Follow-up Questions
  • Why do weights diverge without regularization on separable data?
  • How would you switch to mini-batch stochastic gradient descent?
  • How would you add L1 instead?
  • How do you choose lambda?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Explore ML Interview Prep
Sample Answer
Clarify

Confirm the inputs (dense numeric features already standardized), the label (clicked the offer or not), and whether vectorized libraries like NumPy are allowed. I will write plain Python for clarity....

Approach

Objective: J(w,b)=1n∑i[−yilog⁡pi−(1−yi)log⁡(1−pi)]+λ2∥w∥2J(w, b) = \frac{1}{n}\sum_i \left[-y_i \log p_i - (1 - y_i)\log(1 - p_i)\right] + \frac{\lambda}{2}\|w\|^2 with pi=σ(w⊤xi+b)p_i = \sigma(w^\top x_i + b). The gradient is $\nabla_w J = \frac{1}{n}\s...


Submit Your Answer
Markdown supported