How do you train and evaluate on imbalanced defect data?
handling class imbalance.
resampling, class weighting, focal loss, and anomaly framing for training; evaluate with precision, recall, PR-AUC, and F-beta, not accuracy.
What's really being asked
The interviewer wants both training tactics for extreme imbalance and, critically, the right metrics, since accuracy is meaningless when defects are rare.
The full answer
On the data and training side: oversample the minority defect class, for example with augmentation or synthetic samples, or undersample the majority; apply class weights so defects contribute more to the loss; use focal loss to focus learning on hard, rare examples; and consider reframing the problem as anomaly or one-class detection that models normal product and flags deviations, which suits cases where defects are diverse and scarce. On evaluation: never trust raw accuracy, because predicting always-good can score 99 percent while catching zero defects. Instead use precision and recall on the defect class, the precision-recall curve and its area, and an F-beta score where beta is chosen by the relative cost of misses versus false alarms; a confusion matrix makes the error types explicit. PR-AUC is preferred over ROC-AUC under heavy imbalance because it focuses on the rare positive class.
The mistakes people make
Reporting accuracy as the headline metric. Ignoring recall, so real defects slip through. Naive oversampling that leaks duplicates into validation, or evaluating on a rebalanced rather than real-distribution test set.
What usually comes next
Why PR-AUC beats ROC-AUC here, threshold selection by cost, SMOTE versus augmentation, and keeping the test set at the true class ratio.
A concrete example
If 1 in 1000 parts is defective, a model that labels everything good is 99.9 percent accurate yet useless. You instead weight the loss toward defects, augment defect images, and report that the system catches, say, most defects at an acceptable false-alarm rate, summarized by PR-AUC and a cost-tuned F-beta.
Interview question
Why is overall accuracy a poor metric for a defect detector when defects are very rare?
- a.A model predicting everything as good scores high while catching no defectsCorrect
- b.Accuracy ignores the majority class entirely
- c.Accuracy only applies to regression problems
- d.Accuracy cannot be computed on imbalanced data
Why? this is the answer
With rare positives, always predicting the majority class yields high accuracy yet zero recall on defects. Accuracy is computable but misleading here, which is why precision, recall, and PR-AUC are used.
Just read this? Test yourself on what you have been reading.
Read the original → developers.google.com
- #class-imbalance
- #anomaly-detection
- #evaluation-metrics
- #focal-loss
- #defect-detection
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles