A curated bank of conceptual, mathematical, and case-study interview questions on K-Nearest Neighbors.
How to use this page
Each question is tagged Easy / Medium / Hard. Try to answer before expanding the accordion. Questions are grouped by theme — Fundamentals, Distance & K, Curse of Dimensionality & Scaling, and Tricky cases.
1. Fundamentals
2. Distance Metrics & Choosing K
3. Curse of Dimensionality & Scaling
4. Tricky / Case-Study Questions
5. Advanced & Applied KNN
Quick-fire one-liners
Training time? O(1) — lazy learner, just stores data.
Default distance? Euclidean (L2).
Odd or even K? Odd, to avoid ties in binary classification.
Rule of thumb for K? K ≈ √n, then validate via CV.
Must do before fitting? Scale features.
Big-n speedup? KD-tree / Ball tree / FAISS / HNSW.
Why poor in high d? Distances concentrate — curse of dimensionality.
# Quick sklearn cheat-sheet for interviews
from sklearn.neighbors import KNeighborsClassifier, KNeighborsRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import GridSearchCV
# Classifier with CV-tuned K and distance weighting
pipe = make_pipeline(StandardScaler(), KNeighborsClassifier())
grid = GridSearchCV(
pipe,
param_grid={
"kneighborsclassifier__n_neighbors": [3, 5, 7, 11, 15, 21],
"kneighborsclassifier__weights": ["uniform", "distance"],
"kneighborsclassifier__metric": ["euclidean", "manhattan"],
},
cv=5, scoring="f1_macro", n_jobs=-1,
).fit(X_train, y_train)
# Regressor
reg = make_pipeline(StandardScaler(), KNeighborsRegressor(n_neighbors=10, weights="distance"))
# Approximate NN for large n
# from faiss import IndexFlatL2 # FAISS for million-scale lookup
# index = IndexFlatL2(d); index.add(X); D, I = index.search(query, k=10)