How does one-hot encoding categorical variables affect distances in KNN?

Quick Overview

Asks how one-hot encoding categorical features changes Euclidean distances in k-nearest neighbors. Tests whether you can work out the cost of a category mismatch, compare it with scaled numeric features, and handle high-cardinality or mixed-type data.

How does one-hot encoding categorical variables affect distances in KNN?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

How does one-hot encoding categorical variables affect distances in KNN? Assume Euclidean distance and a dataset that mixes categorical and numeric features. ```hint Compute one mismatch Take two points that differ only in one categorical feature and work out how much that difference adds to the squared Euclidean distance. ``` ```hint Compare with the other features Ask how that fixed amount compares with a typical gap on a standardized numeric feature, and what changes when a variable has hundreds of levels. ``` ### What a Strong Answer Covers - The distance contribution of a category mismatch versus a match, and the fact that every pair of categories ends up equally far apart. - Why one-hot encoding suits nominal variables better than label encoding. - How the fixed mismatch penalty compares with numeric features under different scaling choices. - What high-cardinality variables do to dimensionality, compute and the similarity of related categories. - Practical remedies such as weighting the one-hot blocks, mixed-type metrics, or other encodings. ### Follow-up Questions - How would you choose the weight on each one-hot block, and how would you validate that choice? - When would you switch to Gower distance, and how does it score categorical and numeric features? - Does the picture change if you use Manhattan or cosine distance instead of Euclidean? - For a feature like city with thousands of levels, which encoding would you use for KNN, and what leakage risk comes with target encoding?

Overview: Asks how one-hot encoding categorical features changes Euclidean distances in k-nearest neighbors. Tests whether you can work out the cost of a category mismatch, compare it with scaled numeric features, and handle high-cardinality or mixed-type data.

Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

How does one-hot encoding categorical variables affect distances in KNN?

Assume Euclidean distance and a dataset that mixes categorical and numeric features.

What a Strong Answer Covers Guidance

  • The distance contribution of a category mismatch versus a match, and the fact that every pair of categories ends up equally far apart.
  • Why one-hot encoding suits nominal variables better than label encoding.
  • How the fixed mismatch penalty compares with numeric features under different scaling choices.
  • What high-cardinality variables do to dimensionality, compute and the similarity of related categories.
  • Practical remedies such as weighting the one-hot blocks, mixed-type metrics, or other encodings.

Follow-up Questions Guidance

  • How would you choose the weight on each one-hot block, and how would you validate that choice?
  • When would you switch to Gower distance, and how does it score categorical and numeric features?
  • Does the picture change if you use Manhattan or cosine distance instead of Euclidean?
  • For a feature like city with thousands of levels, which encoding would you use for KNN, and what leakage risk comes with target encoding?