BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Supervised models1 of 18 in topic

119. Classify points by majority vote of their nearest neighbours

EasySupervised models
Asked atMetaNetflixOpenAI

k-nearest neighbours has no training step. To label a new point, find the k closest training points and take the most common label.

Write knn_predict(X_train, y_train, X_test, k). Use Euclidean distance. For ties in distance, the lower training index wins (use a stable sort). For ties in the vote, the smallest label wins. Return a list of ints.

Functiondef knn_predict(X_train, y_train, X_test, k)

Example 1

Input: 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.3], [5.5, 5.2]], k = 3

Output: [0, 1]

Example 2

Input: X_train = [[0, 0], [0, 1], [1, 0], [5, 5], [5, 6], [6, 5]], y_train = [0, 0, 0, 1, 1, 1], X_test = [[4, 4]], k = 1

Output: [1]

Example 3

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

Output: [0]

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 3 tests to be accepted.
Hint 1

There is nothing to train: for each test point you just rank all training points by distance and let the k closest ones vote.

Hint 2

For each test point, compute Euclidean distances to every training row, take the k smallest with a stable argsort, count how often each label appears, and choose the label with the most votes.

Hint 3

Use np.argsort(d, kind='stable')[:k] so equal distances keep the lower training index. For the vote, find the max count, then take min over the labels that have that count.

Common mistakes
  • The buggy version sorts by negative distance, which picks the FARTHEST points.
Understand the idea

k-nearest neighbours has no training. To label a new point you find the k closest training points and take a vote, like asking your nearest neighbours which shop is best. Two tie rules make it testable: when distances tie, the lower training index comes first (a stable sort does this), and when votes tie, the smallest label wins.

Worked example

X_train = [[0], [1], [5]], y_train = [0, 0, 1], test point [4]. Distances are 4, 3 and 1. With k = 1 the closest is index 2, label 1. With k = 3 the votes are 0, 0, 1, so label 0. With k = 2 the closest are index 2 (label 1) and index 1 (label 0), a 1 to 1 tie, so the smaller label 0 wins.

sort by Euclidean distance ascending, keep k, majority label with smallest-label tie-break

Sorting by negative distance picks the farthest points, a classic bug. kNN is used in recommendations and as a baseline.

Topics
Supervised models
Companies3
  • Meta a candidate reported this question Machine Learning interview questions, page 10 (KatKat)
  • Netflix the topic appears in interview reports Netflix Machine Learning Engineer Interview Process
  • OpenAI a candidate reported this question OpenAI Interview Process & Questions (2026) (TrueInterview)

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Run one epoch of the perceptron learning ruleEasy
  • Classify by the nearest class centroidEasy
  • Compute information gain for one splitEasy
  • Classify with distance-weighted k nearest neighbour votesEasy
All Supervised models problems
Report
119 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def knn_predict(X_train, y_train, X_test, k):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 3 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS