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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
Logistic Regression From Scratch
00:00
5 left

Logistic Regression From Scratch

HardPython

Problem

CircleUp needs a lightweight binary classifier for scored company signals. Implement batch logistic regression from scratch, without ML libraries, using gradient descent.

Formal Specification

Implement train_logistic_regression(X, y, learning_rate, iterations, l2).

  • X is a list of n feature vectors, each containing d floats.
  • y is a list of n binary labels, where each value is 0 or 1.
  • learning_rate is the gradient descent step size.
  • iterations is the number of full-batch parameter updates.
  • l2 is the nonnegative L2 regularization strength.

Initialize the intercept and all weights to 0.0. For each iteration, compute predictions with the sigmoid function, calculate gradients over the complete dataset, apply L2 regularization to weights only, then update all parameters simultaneously. Return [intercept, weight_0, ..., weight_d-1].

Use a numerically stable sigmoid implementation so large positive or negative scores do not overflow.

Constraints

  • 1 <= len(X) <= 2,000
  • 1 <= len(X[0]) <= 100
  • Every row in X has the same number of features
  • Each feature value is in [-1,000, 1,000]
  • Each y[i] is either 0 or 1
  • 0 < learning_rate <= 1
  • 1 <= iterations <= 10,000
  • 0 <= l2 <= 10

Function Signature

def train_logistic_regression(X, y, learning_rate, iterations, l2):
Interviewer

Your question is Logistic Regression From Scratch. 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.