mlprep
Hard25 min·2 rounds

Implement a decision tree classifier from scratch

Written by the mlprep team · Updated

Interviewer Mode
Interviewer

Implement a decision tree classifier from scratch in Python. Use information gain (entropy) to choose splits. Support both categorical and continuous features. Walk me through the recursive structure and how you'd prevent overfitting.

Ready to compare your reasoning?
Interviewer

Good implementation. Now: what's the connection between this single decision tree and a random forest? What does the "random" in random forest actually refer to, and why does it help?

Ready to compare your reasoning?

The key takeaway unlocks after you reveal an expert answer.

Follow-up Questions to Test Yourself

Interviewers often pivot here once you cover the core concept.

  • How would you modify this implementation to handle missing values — what does XGBoost do differently from sklearn's approach?
  • Implement feature importance computation from a trained tree — both impurity-based and permutation-based methods.
  • Why do gradient-boosted trees (XGBoost, LightGBM) use shallow trees (depth 3-8) while random forests use deep trees? What's the theoretical reason?

How confident do you feel explaining this?

Track your understanding or flag it to revisit before your next loop.

Related Questions