How do Random Forests handle missing values and categorical features in practice?

Quick Overview

Asks how random forests deal with missing values and categorical columns in real libraries such as scikit-learn and R. Tests knowledge of how each implementation behaves, the trade-offs between encodings as cardinality grows, and how to avoid leakage in the preprocessing pipeline.

How do Random Forests handle missing values and categorical features in practice?

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

How do Random Forests handle missing values and categorical features in practice? Assume tabular data with some missing numeric values and several categorical columns, a few of which have hundreds of levels. ```hint Check the library Behavior depends on the implementation. Compare what R's randomForest and scikit-learn each accept as input. ``` ```hint Think about how a tree splits For each encoding, ask how many splits a tree needs to separate one category from the rest, and how often a sparse column can win a split. ``` ### What a Strong Answer Covers - How support for missing values and categories differs across implementations - How native missing-value handling decides where missing rows go, both in training and at prediction time - The trade-offs between ordinal, one-hot and target encoding as cardinality grows - How to keep imputation and encoding from leaking during cross-validation, and how to treat categories never seen in training - How the preprocessing choice affects feature importance ### Follow-up Questions - Why is impurity-based (MDI) importance biased toward high-cardinality features, and what would you use instead? - When is missingness itself informative, and how do you let the forest use it? - How do gradient boosting libraries such as LightGBM and XGBoost handle these two problems differently? - How would you encode a categorical feature with 50,000 levels, such as a merchant id?

Overview: Asks how random forests deal with missing values and categorical columns in real libraries such as scikit-learn and R. Tests knowledge of how each implementation behaves, the trade-offs between encodings as cardinality grows, and how to avoid leakage in the preprocessing pipeline.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

How do Random Forests handle missing values and categorical features in practice?

Assume tabular data with some missing numeric values and several categorical columns, a few of which have hundreds of levels.

What a Strong Answer Covers Guidance

  • How support for missing values and categories differs across implementations
  • How native missing-value handling decides where missing rows go, both in training and at prediction time
  • The trade-offs between ordinal, one-hot and target encoding as cardinality grows
  • How to keep imputation and encoding from leaking during cross-validation, and how to treat categories never seen in training
  • How the preprocessing choice affects feature importance

Follow-up Questions Guidance

  • Why is impurity-based (MDI) importance biased toward high-cardinality features, and what would you use instead?
  • When is missingness itself informative, and how do you let the forest use it?
  • How do gradient boosting libraries such as LightGBM and XGBoost handle these two problems differently?
  • How would you encode a categorical feature with 50,000 levels, such as a merchant id?