What is K-Nearest Neighbors (KNN) and how does it work?

Updated May 16, 2026

Short answer

KNN is a supervised algorithm that predicts a label based on the majority class (or average value) of its nearest neighbors.

Deep explanation

KNN is a lazy learning algorithm that does not build an explicit model during training. Instead, it stores the dataset and computes distances at inference time. Prediction is made by finding the k closest data points using a distance metric like Euclidean distance and aggregating their labels.

Real-world example

Movie recommendation systems suggesting similar movies based on user preferences.

Common mistakes

  • Assuming KNN trains a model like linear regression.

Follow-up questions

  • Why is KNN called a lazy algorithm?
  • What is the role of K?

More K-Nearest Neighbors interview questions

View all →