Overfitting vs Underfitting — ML Technical Interview Questions & Answers
Too-complex vs too-simple models, and how to spot each.
Overfitting vs Underfitting
- Simple meaning: Overfit = the model memorised the training sheet (train 99%, test 60%). Underfit = it is too simple (train and test both weak, say 55%). You want both scores decent and close.
- Picture: A tree that asks 40 weird questions until every student is perfect on train — then a new student is guessed wrong. That is overfit. A line that ignores hours completely is underfit.
- Fix overfit: more data, simpler model, max_depth / regularisation / dropout, early stop. Fix underfit: better features, a richer model, less regularisation.
- Trap: adding features until train is 100%. Trap: calling 60% test “fine” without a baseline (always guess “pass”).
overfit: train ★★★★ test ★☆
underfit: train ★☆ test ★☆
good: train ★★★ test ★★★Technical Interview Tip: Train 99% / test 60% = overfit. Both low = underfit.
Code Examples for Overfitting vs Underfitting
Overfitting vs Underfitting — working example
# Toy train vs test errors (higher = worse)
models = [
{"name": "underfit", "train": 0.30, "test": 0.32},
{"name": "good", "train": 0.08, "test": 0.10},
{"name": "overfit", "train": 0.01, "test": 0.28},
]
for m in models:
gap = m["test"] - m["train"]
print(m["name"], "gap=", round(gap, 2))
# L2 idea: penalty += lambda * w**2 (discourages huge weights)Overfitting vs Underfitting — overfit = low train / high test gap; underfit = high error everywhere; regularization limits capacity.
How to Explain Overfitting vs Underfitting to an Interviewer
How do you spot overfit?
High train, low test Big gap
How do you fix overfit?
More data Simpler model Regularisation