Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Linear Regression in Python

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Linear Regression in Python. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Enlitic research pipelines may need a simple baseline that maps a numeric feature to a continuous measurement. Implement ordinary least-squares linear regression for one feature.

Write fit_linear_regression(xs, ys) to return [slope, intercept] for the line y = slope * x + intercept that minimizes the sum of squared residuals across paired observations.

Formal Specification

  • xs: list of n integer or float feature values.
  • ys: list of n integer or float target values, where ys[i] corresponds to xs[i].
  • Return: a two-element list [slope, intercept] of floats.

Use the centered least-squares equations: m = sum((x - mean_x) * (y - mean_y)) / sum((x - mean_x)^2) and b = mean_y - m * mean_x.

Constraints

  • 2 <= len(xs) == len(ys) <= 100,000
  • Each value is a finite integer or float in [-10^9, 10^9]
  • xs contains at least two distinct values
  • Return values are accepted within 1e-9 absolute or relative tolerance

Function Signature

def fit_linear_regression(xs, ys):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output