Problem. Given training points X_train (n × d) with integer labels y_train, test points X_test (m × d) and k, predict each
test point's label as the most common label among its k nearest training points (Euclidean distance). Break vote ties by the smaller
label.
X_train = [[0, 0], [0, 1], [1, 0], [5, 5], [5, 6], [6, 5]], y_train = [0, 0, 0, 1, 1, 1]
X_test = [[0.2, 0.1], [5.5, 5.2]], k = 3 → [0, 1]
In an ML coding round, the algorithm is never the hard part. What's being tested is whether you can turn "for each pair, compute a distance" into array operations that run in compiled code, avoid an O(n log n) sort when you only need k items, and keep memory bounded. Those three moves recur in every lesson of this module.
ML context. kNN is a baseline classifier, the retrieval step behind "find similar items" (lesson 15.2), and the core of embedding-based classifiers: embed with a pretrained model, then vote among nearest labelled examples. The same distance-matrix trick computes RBF kernels, k-means assignments (lesson 15.4) and contrastive-loss similarity matrices.