Implement SGD for linear regression and derive gradients

Quick Overview

This question evaluates understanding of gradient-based optimization and linear model training, including competency in deriving gradients for mean squared error and implementing stochastic (or mini-batch) gradient descent for parameter estimation.

Implement SGD for linear regression and derive gradients

Company: Amazon

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Technical Screen

## Prompt You are given a dataset of \(n\) 1D samples \(\{(x_i, y_i)\}_{i=1}^n\), where \(x_i\) and \(y_i\) are real numbers. We want to fit a linear model: \[ \hat{y} = a x + b \] by minimizing the mean squared error (MSE). ## Tasks 1. **Define the loss** function for this problem (e.g., MSE over the dataset). 2. Using the chain rule / backprop-style reasoning, **derive the gradients** \(\frac{\partial L}{\partial a}\) and \(\frac{\partial L}{\partial b}\). 3. Describe (and optionally write pseudocode for) how to **train \(a\) and \(b\) using SGD** (or mini-batch SGD): - parameter initialization - per-step gradient computation - update rule - learning rate choice / scheduling - stopping criteria 4. Discuss common **pitfalls and edge cases** (e.g., scaling, divergence, choosing batch size). ## Output / Expected Result After training, return the learned parameters \(a\) and \(b\) that approximately minimize the chosen loss on the provided data.

Quick Answer: This question evaluates understanding of gradient-based optimization and linear model training, including competency in deriving gradients for mean squared error and implementing stochastic (or mini-batch) gradient descent for parameter estimation.

|Home/Machine Learning/Amazon
Amazon logo
Amazon
Dec 15, 2025, 12:00 AM
mediumMachine Learning EngineerTechnical ScreenMachine Learning
10
0

Prompt

You are given a dataset of nn 1D samples {(xi,yi)}i=1n\{(x_i, y_i)\}_{i=1}^n, where xix_i and yiy_i are real numbers.

We want to fit a linear model:

y^=ax+b\hat{y} = a x + b

by minimizing the mean squared error (MSE).

Tasks

  1. Define the loss function for this problem (e.g., MSE over the dataset).
  2. Using the chain rule / backprop-style reasoning, derive the gradients La\frac{\partial L}{\partial a} and Lb\frac{\partial L}{\partial b} .
  3. Describe (and optionally write pseudocode for) how to train aa and bb using SGD (or mini-batch SGD):
    • parameter initialization
    • per-step gradient computation
    • update rule
    • learning rate choice / scheduling
    • stopping criteria
  4. Discuss common pitfalls and edge cases (e.g., scaling, divergence, choosing batch size).

Output / Expected Result

After training, return the learned parameters aa and bb that approximately minimize the chosen loss on the provided data.

Loading comments...