Decision Tree — ML Technical Interview Questions & Answers
A tree of if/else splits learned from data.
Decision Tree
- Simple meaning: A decision tree is a flowchart the data learns: if marks < 40 → fail, else if attendance < 60 → fail, else pass. Each split is one question on one feature.
- Picture: You can read the path for one student. That is why trees are easy to explain to a teacher. Depth 20 on 80 rows will memorise names — overfit.
- Splits try to make groups “pure” (Gini or entropy). sklearn: DecisionTreeClassifier(max_depth=3) is a sane start.
- Trap: unlimited depth. Trap: thinking a tree needs feature scaling (it does not, unlike KNN/SVM).
Technical Interview Tip: Flowchart of questions. Keep it shallow or it memorises.
Code Examples for Decision Tree
Decision Tree — working example
def majority_vote(labels):
return max(set(labels), key=labels.count)
neighbors = [1, 1, 0, 1, 0]
print(majority_vote(neighbors)) # kNN-style vote
# Logistic idea: score → probability via sigmoid
def sigmoid(z):
return 1 / (1 + 2.718281828 ** (-z))
print(round(sigmoid(0.0), 3), round(sigmoid(2.0), 3))Decision Tree — classifiers map features to labels; kNN votes neighbors; logistic uses a sigmoid probability.
How to Explain Decision Tree to an Interviewer
What is a decision tree?
If/else splits from data Readable path
How do you stop overfit?
max_depth min_samples_leaf prune