Encoding interview question

Preprocessing & Feature EngineeringEncodingbasic

What does one-hot encoding do to a categorical feature such as color with the values red, green and blue?

  1. AIt replaces each color with an integer code such as 0, 1 and 2, keeping the feature in one numeric column.
  2. BIt replaces each color with the mean target value of its rows, so the feature becomes one numeric column.
  3. CIt creates one binary column per color, set to 1 for the row's own color and to 0 in the remaining columns.
  4. DIt replaces each color with the share of training rows that have that color, giving one numeric column.
Show the answer and explanation

Correct answer: C, It creates one binary column per color, set to 1 for the row's own color and to 0 in the remaining columns.

Explanation Encoding

One-hot encoding turns a categorical feature with k categories into k binary indicator columns (color_red, color_green, color_blue). For a known category exactly one of them is 1 and the rest are 0, so no order or distance between categories is implied.

Why the other descriptions are different encoders:

  • Integer codes in a single column are label / ordinal encoding, which implies an order.
  • Replacing a category by its mean target is target encoding.
  • Replacing a category by its share of rows is frequency (count) encoding.
colorcolor_redcolor_greencolor_blue
green010

For a linear model with an intercept, keeping all k columns makes them perfectly collinear (they always sum to 1), so one column is usually dropped (k − 1 dummies).

Interview tip: mention the dummy variable trap and that one-hot becomes impractical when the feature has thousands of categories.

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 Preprocessing & Feature Engineering questions

  1. In model training and evaluation, what does the term data leakage mean?
  2. What is a main reason to perform feature selection before training a model?
  3. A temperature sensor produced 20 readings: 17 between 48 and 52, plus 99, 100 and 101. The table shows their summary statistics. A rule that flags z > 3 using…
  4. A colleague reports a cross-validated AUC of 0.91 with this code on 200 customers and 10,000 features. As a sanity check you rerun it with randomly permuted…
  5. For an offline consumption report (not a forecasting feature), an analyst fills gaps in a building's daily energy readings. The readings were logged on…