Implement K-means and run two iterations
TikTokData Scientist · Technical ScreenLast updated
This question evaluates understanding of k-means clustering concepts (k-means++ initialization, Lloyd iterations, and SSE) and proficiency in numerical implementation and vectorized array operations.
Given points P={(0,0),(0,2),(2,0),(2,2),(8,8),(8,10),(10,8),(10,10)} and k=2, (1) initialize centroids with k-means++ using seed=42 and Euclidean distance; show the sampled centers. (2) Perform exactly two Lloyd iterations (assign → update) by hand, breaking ties by choosing the lower-index centroid; list cluster memberships and centroids after each iteration. (3) Compute SSE after the second iteration. (4) Describe how you would implement this efficiently in vectorized NumPy (no loops over points), including the time complexity per iteration, and explain how you would handle empty clusters robustly.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.