Code Roomk-nearest-neighbors classification
MediumPrep Room Coding #1539

k-nearest-neighbors classification

CodingML systemsMid–Senior~25 min

Implement k-nearest-neighbors classification. Given a list of training points (each a list of floats), a parallel list of integer labels, a query point, and an integer k, return the predicted label: the most common label among the k training points closest to the query by Euclidean distance. There are at least k points. Break distance ties by preferring the earlier point (lower index). Break label-vote ties by choosing the smallest label value.

Implement
knn_classify(points: list[list[float]], labels: list[int], query: list[float], k: int) → int
Examples
in[[[0,0],[1,1],[5,5]],[0,0,1],[0,0],1]out0
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 25 min
InputExpectedGot
[[[0,0],[1,1],[5,5]],[0,0,1],[0,0],1]0not run yetsample