AO
AceOffer
·
Back

K-Means Clustering Implementation

Phone ScreenPhone ScreenSoftware Engineer, Machine Learning EngineerLast reported October 2025Low Frequency

Problem Overview

Given a 2D array of coordinate points where each element is [row, col], and an integer k representing the number of clusters, implement K-Means clustering from scratch.
Full problem statement
Given a 2D array of coordinate points where each element is [row, col], and an integer k representing the number of clusters, implement K-Means clustering from scratch. Initialize centroids using the first k points in the array. Iteratively assign each point to its nearest centroid and recompute centroids as the mean of their assigned points. Continue until convergence. Return the clustered results (points grouped by cluster). Emphasis is placed on using numpy broadcasting and vectorization for performance optimization.

Follow-up Arc

Interviewers escalate through these phases. The order varies, but most candidates see at least one from each bucket.
Trade-off discussion · 3
Trade-off discussion
Can you optimize this using numpy broadcasting to avoid explicit Python loops?
Probes for: Candidate writes a pure Python loop-based solution
How do you determine convergence? What stopping criteria would you use?
Probes for: After base implementation is complete
Would you prefer to write this in Python or C++? (or: does the solution need to work in C++?)
Probes for: Implementation is working

Approach Trade-offs

Approaches actually attempted in reports — including ones that lost candidates time. Pick deliberately.
ApproachNotes
Pure Python (no numpy)Simpler to reason about but significantly slower for large datasets; not preferred by interviewers who explicitly test for vectorization.
Random centroid initialization (K-Means++)Better convergence properties in practice, but the interview specifies using the first k points as initial centroids, so this may be out of scope unless asked.

Practice

Open the editor to write a solution against test cases, then return here to compare against the follow-ups.
Open Editor →
Waymo · Phone Screen · Reported 2× across candidate reports