K-Nearest Neighbors (KNN) — ML Technical Interview Questions & Answers
Classifying a point by the majority vote of its neighbors.
K-Nearest Neighbors (KNN)
- Simple meaning: KNN does not learn a formula. For a new student, find the k closest rows on the sheet and vote (class) or average (marks). Lazy: work happens at predict time.
- Picture: k=5. Four nearby students passed, one failed → guess pass. If hours is 1–10 and income is 50,000, distance is dominated by income — scale first.
- sklearn: KNeighborsClassifier(n_neighbors=5). Small k = jumpy (variance). Large k = bland (bias). Odd k helps tie-break in binary class.
- Trap: no scaling. Trap: k=1 on a noisy sheet (memorise).
Technical Interview Tip: Look at neighbours. Scale. k too small = jumpy.
Code Examples for K-Nearest Neighbors (KNN)
K-Nearest Neighbors (KNN) — working example
def majority_vote(labels):
return max(set(labels), key=labels.count)
neighbors = [1, 1, 0, 1, 0]
print(majority_vote(neighbors)) # kNN-style vote
# Logistic idea: score → probability via sigmoid
def sigmoid(z):
return 1 / (1 + 2.718281828 ** (-z))
print(round(sigmoid(0.0), 3), round(sigmoid(2.0), 3))K-Nearest Neighbors (KNN) — classifiers map features to labels; kNN votes neighbors; logistic uses a sigmoid probability.
How to Explain K-Nearest Neighbors (KNN) to an Interviewer
Does KNN train a line?
No Stores the sheet Votes at predict time
Why scale?
Distance Big-range columns dominate