137. Run one iteration of k-means
k-means alternates two steps: give every point to its nearest centroid, then move each centroid to the mean of its points.
Write kmeans_step(X, centroids) that does one full iteration and returns (labels, new_centroids). labels is a list of ints. Ties go to the lower centroid index. If a centroid gets no points, keep it where it was.
def kmeans_step(X, centroids)Example 1
Input: kmeans_step([[0.0], [1.0], [9.0], [10.0]], [[0.0], [5.0]])
Output: ([0, 0, 1, 1], [[0.5], [9.5]])
Example 2
Input: kmeans_step(np.array([[0.0, 0.0], [0.0, 1.0], [10.0, 10.0], [10.0, 11.0], [11.0, 10.0]]), [[0, 0], [10, 10]])[0]
Output: [0, 0, 1, 1, 1]
Example 3
Input: kmeans_step(np.array([[0.0, 0.0], [0.0, 1.0], [10.0, 10.0], [10.0, 11.0], [11.0, 10.0]]), [[0, 0], [10, 10]])[1]
Output: [[0.0, 0.5], [10.333333, 10.333333]]
Constraints
- Python 3 with NumPy. Code runs in your browser.
- Your code must pass all 3 tests to be accepted.
Hint 1
One iteration is two separate steps: assign every point to its closest centroid, then recompute each centroid as the mean of its assigned points.
Hint 2
Compute squared distances between every point and every centroid with broadcasting, take np.argmin over centroids for labels, then loop over centroids and average the points with that label.
Hint 3
Distances come from X[:, None, :] - C[None, :, :] summed over the last axis, argmin already picks the lowest index on ties, and start from C.copy() so empty clusters keep their old position.
Common mistakes
- The buggy version moves it to the origin.
Understand the idea
k-means groups points around centres by repeating two steps. First, each point joins its nearest centroid. Second, each centroid moves to the average of the points that joined it. Think of shops moving to the middle of their regular customers. Squared distance is enough for choosing the nearest, since the square root does not change the order. Worked example, 1D: X = [0, 1, 9, 10], centroids [0, 5]. Point 0 is nearest centroid 0. Point 1 is 1 from centroid 0 and 4 from centroid 1, so centroid 0. Points 9 and 10 go to centroid 1. New centroids: mean(0, 1) = 0.5 and mean(9, 10) = 9.5. Labels [0, 0, 1, 1]. A point exactly halfway goes to the lower index. If a centroid gets no points, it has no mean, so leave it where it was instead of moving it to zero.
assign by argmin of squared distance, then centroid = mean of its members. k-means is used to cluster embeddings and build vector index partitions
Topics
Companies7
- Amazon a candidate reported this question Amazon AI Research Scientist interview experience (February 2025)
- Anthropic the topic appears in interview reports Coding Exercises & ML System Design: Real AI Interview Questions (landedjobs)
- ByteDance a candidate reported this question ByteDance Machine Learning Engineer Interview Process
- Google the topic appears in interview reports Google Interview Prep (Deep-ML)
- Meta a candidate reported this question Meta Research Engineer Interview Guide (Dataford)
- Microsoft a candidate reported this question Microsoft Senior Applied Scientist (L63) Interview Experience
- OpenAI a candidate reported this question OpenAI interview questions, page 54 (Screna)
From public interview reports and guides. Not affiliated with or endorsed by these companies.