Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Linear Regression in NumPy
00:00
5 left

Linear Regression in NumPy

HardPython

Problem

Write a code for linear regression in bare NumPy.

Implement ordinary least squares using only NumPy, automatically adding an intercept term and supporting multiple features. The implementation should remain defined for rank-deficient or underdetermined design matrices by returning the minimum-norm least-squares solution. Input: X, a numeric 2D array of shape (n_samples, n_features), and y, a numeric 1D array of shape (n_samples,). Output: a 1D array of shape (n_features + 1,), ordered as intercept followed by feature coefficients.

Constraints

  • 1 <= n_samples <= 5000
  • 1 <= n_features <= 100
  • X and y contain finite numeric values
  • len(y) == number of rows in X
  • The output includes an intercept before all feature coefficients

Function Signature

def linear_regression(X, y):
Interviewer

Your question is Linear Regression in NumPy. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.