Live coding: implement the k-means clustering algorithm on the spot.
Asked in the onsite_virtual stage. English gloss: live-code an implementation of the k-means clustering algorithm from scratch. Round 2, candidate did not finish it.
Implement k_means(points, k, max_iterations). points is a non-empty list of equal-length numeric lists. Initialize centroids using evenly spaced point indices, including the first and last point. For k == 1, use the first point as the initial centroid. Assign each point to the nearest centroid using squared Euclidean distance, breaking ties by the lowest centroid index. Recompute each non-empty centroid as the coordinate-wise mean. Preserve the previous centroid for an empty cluster. Return centroids after convergence or after max_iterations updates.
def k_means(points, k, max_iterations):