13 interview questions on decision trees & random forests, each answered in full. Free to read, no account needed.
Random forest vs gradient boosting — which do you pick and why?
medium- Random forest averages many deep trees trained on bootstrapped samples with random feature subsets — it reduces variance and is robust with little tuning.
- Gradient boosting (XGBoost, LightGBM, CatBoost) fits trees sequentially, each correcting residuals — it usually gives better accuracy on tabular data but needs more tuning and is more prone to overfitting.
- For a strong baseline on tabular data: GBDT wins.
- For a fast, safe baseline: random forest.
Gini impurity, entropy, and classification error — which do trees actually use?
medium- For classification, trees choose the split that most reduces impurity.
- Gini = 1 - sum pc2 and entropy = -sum pc log pc are almost interchangeable in practice, both being smooth and differentiable proxies for classification error.
- Gini is slightly faster (no log) and is CART's default.
- Classification error is not smooth enough — it's rarely used to grow trees, only to prune them.
- For regression, the criterion is variance reduction (MSE).
How do decision trees handle categorical features?
medium- Native support depends on the library.
- Classic CART considers only binary splits: partition category set A vs the rest, or the exhaustive best partition (2(k−1)−1possibilities,expensiveforhighcardinality). scikit-learn does *not* support categoricals natively — you must one-hot encode.
- LightGBM has native categorical splits via Fisher's optimal partitioning.
- CatBoost uses ordered target encoding.
- XGBoost added native categorical support in 1.5+.
- Native handling is usually stronger and faster than one-hot for high-cardinality categoricals.
What are the most impactful hyperparameters of a decision tree?
easy- maxdepth: hard limit on depth — the biggest lever. minsamplessplit: minimum samples needed to consider splitting a node. minsamplesleaf: minimum in each leaf — controls leaf size and variance. maxfeatures: number of features to consider per split (=all for a tree, sqrt(d) or similar for a forest). minimpuritydecrease: don't split unless impurity drops by at least this much.
- Together they control the bias-variance tradeoff and inference latency.
How do decision trees handle missing values?
medium- Options: (1) surrogate splits (CART's approach): find a backup feature that best mimics the chosen split's partition — used when the primary feature is missing at prediction time.
- (2) Default direction (XGBoost, LightGBM): during training, learn per split which direction — left or right — missing values should go.
- (3) Sentinel value: encode missingness as a specific value (e.g., -9999) and let the tree branch on it — works but hides information.
- (4) Impute first then train.
What criterion does a regression tree use to choose splits?
easy- Variance reduction: pick the split that most reduces sum of squared errors of the target within child nodes.
- Equivalently, maximize the total variance of the parent minus the weighted sum of children variances.
- The prediction in each leaf is the mean of the target for training points that fall into it (median for MAE-loss trees).
- Same tree machinery, different impurity function.
How is feature importance computed from a decision tree or random forest?
medium- Impurity-based (MDI): sum, over all splits using that feature, of the impurity decrease weighted by the number of samples reaching the split.
- Cheap and comes free from training.
- Problem: biased toward high-cardinality and continuous features.
- Permutation importance: shuffle a feature's values on a held-out set and measure the drop in metric — unbiased, model-agnostic, more expensive.
- SHAP values: consistent, additive, per-example — the gold standard when you can afford them.
How does bagging reduce variance?
medium- Bagging (bootstrap aggregating) trains B models on B bootstrap samples of the training set and averages predictions.
- If the models were independent with variance σ2, the average has variance σ2/B.
- In practice, trees trained on bootstrapped data are positively correlated (they share most of the data), so the variance reduction is bounded by the correlation rho: Var ≈ ρ⋅σ2+(1−ρ)⋅σ2/B.
- Random Forest reduces rho by also sampling features per split.
Why does Random Forest sample features at each split, not just once per tree?
medium- Per-split feature subsampling forces different trees to explore different features and different splits.
- Without it, if one feature is very predictive, every tree would keep splitting on it near the root and become highly correlated — averaging correlated trees barely reduces variance.
- Sampling features at each split de-correlates the ensemble.
- Default maxfeatures=sqrt(d) for classification, d/3 for regression is a well-tested rule of thumb.
How do Extra Trees differ from Random Forests?
medium- Two changes: (1) each tree is trained on the whole training set (no bootstrapping); (2) splits are chosen randomly — for each candidate feature, a random split threshold is drawn rather than the optimal one, and the best of those random splits is picked.
- Result: more randomness, more bias, less variance, and *faster* training.
- Typically slightly worse than RF on well-tuned problems but sometimes better on very noisy data and always faster.
Which Random Forest hyperparameters actually matter for tuning?
medium- nestimators: 'more is better' until returns diminish — 200-500 is a solid default. maxfeatures: the main lever for correlation between trees; try sqrt(d), log2(d), or 0.3-0.5*d. maxdepth/minsamplesleaf: control tree size; deeper trees favor bias-reduction, larger minsamplesleaf reduces variance. classweight='balanced' for imbalanced data. njobs=-1 for speed.
- Skip fiddling with minsamplessplit — minsamplesleaf is enough.
Are Random Forest probability estimates well-calibrated?
medium- Not always.
- RF probabilities come from averaging tree votes and tend to be pushed away from 0 and 1 (over-cautious).
- A single deep tree gives 0/1 predictions; averaging many gives probabilities that live mostly in a compressed middle range.
- For calibrated probabilities, apply isotonic or Platt calibration on a held-out set (sklearn's CalibratedClassifierCV).
- GBDTs are usually better-calibrated out of the box.
What is Boruta and when do you reach for it?
hard- A wrapper feature selection algorithm that compares each feature's importance to that of 'shadow' features — random permutations of that feature.
- A feature is confirmed if its importance is significantly higher than the max shadow importance across multiple random forest fits (statistical test with Bonferroni correction).
- Slow (many RF fits) but very rigorous — often used in biology / genomics where reliability matters more than speed.
- Use when you need statistical confidence that selected features are truly informative.