Random Forest vs Gradient Boosting — Interview Answer — ML Technical Interview Questions & Answers
Model answer comparing bagging and boosting.
Random Forest vs Gradient Boosting — Interview Answer
- Say this: “Random forest bags trees in parallel and votes — lowers variance. Gradient boosting adds trees in a sequence to fix leftover error — usually stronger on tables, easier to overfit if you add endless rounds.”
- Forest: robust, fewer knobs. Boosting (XGBoost): more knobs, need early stop / small learning rate.
- Both are ensembles of trees. Neither is magic on leaked data.
- Trap: “boosting is always better”. Trap: “forest cannot overfit”.
Technical Interview Tip: Parallel vote vs queue of fixers. Boosting needs a stop rule.
Code Examples for Random Forest vs Gradient Boosting — Interview Answer
Random Forest vs Gradient Boosting — Interview Answer — working example
# ROC idea: vary threshold on scores
y_true = [0, 0, 1, 1]
scores = [0.1, 0.4, 0.35, 0.8]
def tpr_fpr(threshold):
pred = [1 if s >= threshold else 0 for s in scores]
tp = sum(t == 1 and p == 1 for t, p in zip(y_true, pred))
fp = sum(t == 0 and p == 1 for t, p in zip(y_true, pred))
fn = sum(t == 1 and p == 0 for t, p in zip(y_true, pred))
tn = sum(t == 0 and p == 0 for t, p in zip(y_true, pred))
tpr = tp / (tp + fn)
fpr = fp / (fp + tn)
return tpr, fpr
for thr in (0.3, 0.5, 0.75):
print(thr, tpr_fpr(thr))
# Boosting: train trees sequentially on residuals / hard examplesRandom Forest vs Gradient Boosting — Interview Answer — rOC plots TPR vs FPR across thresholds; AUC summarizes ranking quality. Boosting builds strong learners from weak ones.
How to Explain Random Forest vs Gradient Boosting — Interview Answer to an Interviewer
Random forest idea?
Bagging Vote
Boosting idea?
Each tree fixes the last