AO
Back

K-Means Clustering Implementation

Phone ScreenPhone ScreenLast reported October 2025Low Frequency

Problem Overview

Two first-hand phone-screen reports. In one, on the company's own coding pad, there were two clustering-themed problems and both had to be finished. The first: given a 2D array whose elements are points [row, col] and an integer k, split the points into k clusters. The initial centroids can be the first k points; update each cluster's centroid round by round, and return the clustered coordinates. The other report, from an MLE phone screen, says the round was hand-written k-means, with a lot of weight on simplifying and speeding it up with numpy broadcasting.

Part 1 — K-means on grid points

Write k_means(points, k, max_iters=100).

  • Start with the first k points as the centroids.
  • Each round: assign every point to its nearest centroid (Euclidean distance), then move each centroid to the mean of the points assigned to it.
  • Stop when no point changes cluster, or after max_iters rounds.
  • Return k lists of points. Cluster j is the one whose centroid started at points[j]; each list keeps the input order.

numpy is available. The function name, the return shape and the stopping rule are our setup; the tests avoid points that are equally close to two centroids, and clusters that end up empty.

Approach Trade-offs

Approaches actually attempted in reports — including ones that lost candidates time. Pick deliberately.
ApproachNotes
Pure Python (no numpy)Same result and easier to reason about, but slower; one report says the interviewer put a lot of weight on numpy broadcasting.
Random or k-means++ initializationUsually converges to better clusters, but the result changes between runs; one report gives the first k points as the starting centroids.

What Reports Emphasize

Interviewer hints: One MLE phone-screen report says the round put a lot of weight on simplifying and speeding up the code with numpy broadcasting.; In one report the screen had two clustering-themed problems and both had to be finished; the second groups the [row, col] positions of each character in a multiline string.

Practice

Write your own against 5 test cases, or read the worked solution — approach, complexity, and code that runs.
Did this match your interview?

If you have had this round, tell us how close this page was. It is how we keep the pages accurate.

Waymo · Phone Screen · Reported 2× across candidate reports
Is this helpful?