PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of linear regression, mean squared error loss, gradient computation, parameter updates, and proficiency with vectorized numerical operations used in batch gradient descent.

  • N/A
  • Coding & Algorithms
  • Machine Learning Engineer

Implement Linear Regression Training

Company: N/A

Role: Machine Learning Engineer

Category: Coding & Algorithms

Interview Round: Onsite

Implement a function `train_linear_regression(X, y, lr, epochs)` that trains a linear regression model from scratch using batch gradient descent. Requirements: - `X` is an `n x d` matrix of real-valued features. - `y` is a length-`n` vector of real-valued targets. - Initialize all weights and the bias to `0`. - On each epoch, compute predictions `y_hat = Xw + b`. - Use mean squared error as the loss. - Compute the gradients for `w` and `b`. - Update parameters with the given learning rate. - Return the final weight vector and bias. Do not call a library routine that directly fits a regression model. Basic matrix or array operations are allowed.

Quick Answer: This question evaluates understanding of linear regression, mean squared error loss, gradient computation, parameter updates, and proficiency with vectorized numerical operations used in batch gradient descent.

Train linear regression from zero weights using batch gradient descent and return [weights,bias] rounded to 6 decimals.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: ([[1.0],[2.0]], [2.0,4.0], 0.1, 2)

Expected Output: [[1.32], 0.78]

Explanation: Two epochs from zero initialization.

Input: ([[1.0,0.0],[0.0,1.0]], [1.0,2.0], 0.05, 3)

Expected Output: [[0.121625, 0.26425], 0.385875]

Explanation: Multiple features are updated by batch gradients.

Input: ([], [], 0.1, 5)

Expected Output: [[], 0.0]

Explanation: Empty data returns zero parameters.

Hints

  1. Clarify edge cases before coding.
  2. Keep the return value deterministic.
Last updated: Jun 27, 2026

Related Coding Questions

  • Compare Strings With Deletions - N/A (medium)

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.