K-Means Clustering from Scratch

Asked at Apple · Coding Questions · Difficulty: Medium · Last asked Mar 25, 2026

Implement K-Means clustering (Lloyd's algorithm) from scratch using only the Python standard library. Given a set of points, group them into `k` clusters by alternately assigning every point to its nearest centroid and moving every centroid to the mean of the points assigned to it.

Loading the problem…