Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

ML coding · Classic algorithms

k-nearest neighbours classifier

Easy · Target 12 minutes · Python and NumPy

Given training points X_train (n × d) with integer labels y_train, predict a label for each row of X_test using Euclidean distance and the k nearest training points.

  • Training points at equal distance are ordered by their index in X_train, lower first.
  • The prediction is the label with the most votes among the k. If several labels tie, choose the one whose nearest member comes first in that order.

Coordinates are integers, so equal distances are exactly equal.

Examples

Input X_train = [[0, 0], [1, 0], [5, 5], [6, 5]], y_train = [0, 0, 1, 1], X_test = [[0.5, 0], [5.5, 5]], k = 1

Output [0, 1]

Each test point copies its single nearest neighbour.

Input X_train = [[0, 0], [2, 0], [4, 0]], y_train = [1, 2, 2], X_test = [[1, 0]], k = 3

Output [2]

k covers every point: label 2 wins two votes to one.

Input X_train = [[0, 0], [2, 0], [10, 0]], y_train = [7, 3, 3], X_test = [[1, 0]], k = 2

Output [7]

A tie at distance 1. Index order puts label 7 first, so 7 wins the 1–1 vote.

Constraints

  • 1 ≤ k ≤ n ≤ 2000, 1 ≤ d ≤ 50, up to 500 test points
  • Integer coordinates in the training set

Hints

Hint 1

Build the full distance matrix with broadcasting: (n_test, 1, d) minus (1, n, d).

Hint 2

Squared distances give the same order as distances. np.argsort with kind="stable" keeps ties in index order.

Hint 3

Count votes in a dict filled in neighbour order. Python dicts remember insertion order, which is exactly the tie rule.

Where candidates go wrong

  • np.argsort with the default quicksort, which does not promise to keep equal distances in index order.
  • Breaking vote ties by the smallest label, or by Counter.most_common, whose order among equal counts is insertion order of the whole training set, not of the neighbours.
  • Taking a square root that changes nothing but costs time and can create float differences between equal integer distances.
Console⌘/Ctrl + Enter runs

Python runs in your browser with NumPy. The first run downloads the runtime, which takes a few seconds.


Why interviewers ask it

kNN is the simplest model there is, so the interview is about engineering: a vectorised distance matrix, the choice of squared distance, and clear rules for ties. Candidates who never think about ties fail the hidden tests.

The theory behind it is in the lesson k-means, kNN, PCA and decision trees from scratch.


Keep going