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
Implementing an ML Algorithm
00:00
5 left

Implementing an ML Algorithm

HardPython

Problem

Darwill needs a deterministic clustering routine for grouping audience feature vectors before campaign analysis. Implement k-means without external machine learning libraries.

Given n points in d dimensions and k initial centroids, repeatedly assign each point to its nearest centroid and recompute each centroid as the coordinate-wise mean of its assigned points.

Use squared Euclidean distance. If a point is equally close to multiple centroids, assign it to the centroid with the smallest index. If a centroid receives no points during an iteration, retain its previous coordinates. Stop when the largest squared centroid movement is at most tolerance², or when max_iterations is reached. Finally, assign every point using the returned centroids.

Return a dictionary with centroids, a list of final centroid vectors, and assignments, a list containing each point's centroid index.

Formal Specification

Implement k_means(points, initial_centroids, max_iterations, tolerance). points and initial_centroids are nonempty lists of equal-dimensional numeric vectors. initial_centroids contains k vectors. Return {"centroids": list[list[float]], "assignments": list[int]}.

Constraints

  • 1 <= n <= 10^4, where n is the number of points
  • 1 <= k <= min(n, 100)
  • 1 <= d <= 50, where d is the vector dimension
  • 1 <= max_iterations <= 1,000
  • tolerance >= 0
  • All vectors have the same dimension
  • All coordinates are finite numeric values

Function Signature

def k_means(points, initial_centroids, max_iterations, tolerance):
Interviewer

Your question is Implementing an ML Algorithm. 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.