KNN interview question

Machine LearningKNNText Featuresmedium

A helpdesk routes tickets with a KNN classifier using Euclidean distance on TF-IDF vectors built with norm=None (no length normalisation). Tickets range from 15 to 3,000 words. Short tickets end up with short neighbours whatever their topic, and long tickets seem far from everything. Which change fixes this?

  1. ASwitch to Manhattan distance: summing absolute differences instead of squaring them makes the distance insensitive to how long each ticket is, so topics decide again.
  2. BStandardise every term column to zero mean and unit variance; once all terms share one scale, the length of a ticket no longer affects the distances between tickets.
  3. CUse cosine distance, or L2-normalise the vectors so Euclidean ranks neighbours the same way; both compare term proportions, so short and long tickets on one topic end up close.
  4. DRaise k so that long tickets also gather enough neighbours; Euclidean distance works well for text once each neighbourhood is large enough to average out the document-length effect.
Show the answer and explanation

Correct answer: C, Use cosine distance, or L2-normalise the vectors so Euclidean ranks neighbours the same way; both compare term proportions, so short and long tickets on one topic end up close.

Explanation KNN

Without normalisation, a document's vector grows with its length: a 3,000-word ticket has much larger term weights than a 15-word one. Euclidean distance then mostly measures magnitude: two short tickets on different topics are close because both sit near the origin, and long tickets are far from everything.

Cosine distance, 1 − a·b / (‖a‖‖b‖), depends only on the direction of the vectors, i.e. the relative proportions of the IDF-weighted terms, so a short and a long ticket about the same problem end up close. For L2-normalised vectors ‖a − b‖² = 2 − 2·cos(a, b), so Euclidean distance after normalisation ranks neighbours exactly as cosine does. (scikit-learn's TfidfVectorizer uses norm="l2" by default for this reason.)

Manhattan versus Euclidean is a different trade-off: Euclidean squares each coordinate difference, so one large difference dominates; Manhattan grows linearly and is less dominated by it.

Difference vectorManhattan (L1)Euclidean (L2)
(4, 0, 0, 0): one large gap44
(1, 1, 1, 1): four small gaps42

But both still scale with document length, so switching to L1 does not fix the problem.

Why the other fixes fail: standardising columns rescales each term across documents but leaves each document's overall length effect in place, and it destroys sparsity. A larger k changes how many neighbours vote, not which documents count as near.

Interview tip: the same rule applies to embeddings: most sentence-embedding models are trained for cosine similarity, so normalise them and a dot product equals cosine.

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More Machine Learning questions

  1. For a declined credit-card application, an Anchors explainer returns the rule IF utilization > 0.80 AND missed_payments_12m ≥ 2 THEN decline, with precision…
  2. A payments company's fraud team reports F1 and ROC-AUC, yet trains its classifier with gradient descent on log loss. A product manager asks: "Why not minimise…
  3. A food-delivery app ranks restaurants with a Wide & Deep model: the wide part is a linear model on crossed features such as user_last_cuisine ×…
  4. A telecom wants k = 20 segments for 50 million subscribers described by 40 numeric features stored as float64. Full-batch k-means (Lloyd's algorithm)…
  5. An e-commerce team trains pricing agents in a simulator and keeps a large buffer of transitions collected by older versions of its policies. Which grouping of…