Loading…
K-Nearest Neighbors Classifier is a medium coding interview question covering machine learning, vector search, arrays — the kind asked at FAANG (Facebook/Meta, Apple, Amazon, Netflix, Google) and AI-first companies like Anthropic, OpenAI, and Nvidia.
Implement a K-Nearest Neighbors (KNN) classifier from scratch.
Given a training set of (feature_vector, label) pairs and a query point, classify the query by majority vote among its k nearest neighbors using Euclidean distance.
Example: train=[([0,0],0),([5,5],1),([1,1],0)], query=[2,2], k=2 -> label 0
This is a fundamental non-parametric ML algorithm. No training phase -- all work is at inference time.
Solution walkthrough on Pro
Upgrade to Pro to unlock the guided hints, the step-by-step walkthrough, solutions in Python, C++, Java, and Go, and complexity analysis.
What's inside this guide
Fully unlocked, no account needed — solutions, walkthroughs and follow-up answers included.
Or start the Study Plan — a week-by-week order for your loop, and week 1 is entirely free.
A full interview loop is more than one round. These are the others, each with what is on the site for it.