Code Room
CodingMediumcod-g976
Subject Ml algorithmsLevel Mid–Senior~25 minCommon in ML systems interviewsIndustries Software development, Technology

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.