Classification Models Interview Questions for Machine Learning
25 Classification Models questions with answers for machine learning roles — 11 easy, 9 medium, 5 hard.
About Classification Models
Classification predicts categories such as fraud, churn or spam. This topic covers logistic regression, k-NN, Naive Bayes and SVMs, the confusion matrix, precision, recall, F1, ROC-AUC and PR-AUC, class imbalance, threshold tuning, probability calibration and multiclass strategies, with practical scikit-learn code that interviewers often ask you to write.
Sheet progressStart practicing
3 QUESTIONS FREESign up to unlock the full question bank and track your progress.Unlock every question and answer, then continue practising company-wise and package-wise.
Logistic regression computes a weighted sum of features and passes it through the sigmoid function, 1 / (1 + e^-z), to get a probability between 0 and 1. It is trained by minimising log loss. Coefficients represent change in log-odds per unit of a feature, and exp(coefficient) gives the odds ratio. A threshold, 0.5 by default, converts probabilities into class labels.
02
Why is logistic regression called regression if it is used for classification?
It models a continuous quantity, the log-odds of the positive class, as a linear function of features, which is regression in the statistical sense. The classification comes only when you apply a threshold to the predicted probability. This is why it outputs well-calibrated probabilities more naturally than many other classifiers.
03
Explain the k-nearest neighbours algorithm and its weaknesses.
k-NN predicts by finding the k closest training points and taking a majority vote for classification or the average for regression. It has no training phase but is slow at prediction on large data, needs feature scaling, suffers from the curse of dimensionality and is sensitive to irrelevant features. Small k overfits, large k underfits; choose k with cross-validation.
Naive Bayes applies Bayes' theorem to compute the probability of each class given the features, assuming features are conditionally independent given the class. That independence assumption is rarely true, hence 'naive', but the model still works well for text classification like spam detection. Variants include Gaussian, Multinomial for word counts and Bernoulli for binary features. It is fast and needs little data.
A confusion matrix is a table of actual versus predicted classes. For binary problems it has true positives, false positives, true negatives and false negatives. From it you derive precision, recall, specificity and accuracy. For example, in fraud detection a false negative is a missed fraud and a false positive is a genuine customer blocked, and these usually have very different costs.
Precision is TP / (TP + FP): of the cases flagged positive, how many were right. Recall is TP / (TP + FN): of the actual positives, how many were found. F1 is the harmonic mean of the two and is low if either is low. Use precision when false alarms are costly, such as spam filters; use recall when misses are costly, such as cancer screening or fraud.
07
A fraud model has 99.6 percent accuracy, but the business says it is useless. Why?
If only 0.4 percent of transactions are fraud, predicting 'not fraud' for everything gives 99.6 percent accuracy and catches nothing. I would switch to recall, precision and PR-AUC, check the confusion matrix, then retrain with class weights or resampling and tune the threshold so the fraud team gets a manageable number of alerts with high recall.
08
You must build a spam classifier quickly with limited compute. What do you choose?
I would use TF-IDF features with a Multinomial Naive Bayes or linear model like logistic regression or LinearSVC. They train in seconds, handle high-dimensional sparse text well and give strong baselines. I would evaluate precision carefully because blocking genuine mail is costly. Only if this is not good enough would I try fine-tuning a small transformer model.
09
Print a confusion matrix and classification report in scikit-learn.
from sklearn.metrics import confusion_matrix, classification_report
pred = clf.predict(X_test)
print(confusion_matrix(y_test, pred))
print(classification_report(y_test, pred, digits=3))
The report shows precision, recall, F1 and support per class plus macro and weighted averages. ConfusionMatrixDisplay.from_estimator draws it as a plot.
10
How do you plot an ROC curve and get AUC in scikit-learn?
from sklearn.metrics import roc_auc_score, RocCurveDisplay
import matplotlib.pyplot as plt
proba = clf.predict_proba(X_test)[:, 1]
print('AUC', roc_auc_score(y_test, proba))
RocCurveDisplay.from_predictions(y_test, proba)
plt.show()
Pass probabilities or decision scores, not hard labels, otherwise the curve has only one point.
11
How do you keep learning new ML techniques while working full time?
I set a small weekly routine: one paper summary or library release note, and one hands-on experiment on a public dataset. I follow scikit-learn and LightGBM release notes, and I rebuild ideas on a small problem before suggesting them at work. Recently I tried TunedThresholdClassifierCV and later used it to replace a manual threshold script in our project.
An SVM finds the hyperplane that separates classes with the maximum margin, and the points on the margin are the support vectors. The soft-margin version uses C to trade off margin width against misclassification. Kernels like RBF or polynomial let SVMs learn non-linear boundaries by implicitly mapping data to higher dimensions. SVMs need scaled features and become slow beyond roughly a hundred thousand rows.
13
What is the ROC curve and AUC? When is PR-AUC better?
The ROC curve plots true positive rate against false positive rate at every threshold, and AUC is the probability that a random positive is ranked above a random negative. It is threshold-independent. On heavily imbalanced data, like 0.5 percent fraud, ROC-AUC can look high even when precision is poor, so the precision-recall curve and PR-AUC give a more honest picture.
Start with the right metrics, such as PR-AUC, recall at fixed precision or F1, not accuracy. Then use class_weight='balanced', resampling like random undersampling or SMOTE applied only on training folds, and threshold tuning based on business cost. Boosting models with scale_pos_weight also help. Stratified splits keep class ratios consistent across folds.
15
How do one-vs-rest and multinomial approaches differ for multiclass classification?
One-vs-rest trains one binary classifier per class against all others and picks the highest score; it is simple but probabilities may not sum to one. Multinomial (softmax) logistic regression models all classes jointly with a single loss, giving consistent probabilities. For metrics, use macro averaging to treat classes equally or weighted averaging to account for class frequency.
16
The fraud team can only review 500 alerts a day. How do you set the threshold?
Instead of 0.5, rank transactions by predicted probability and pick the threshold that yields about 500 alerts per day on recent data. Then report precision and recall at that cut-off, or precision at k. I would monitor daily alert volume because drift can change it, and recompute the threshold periodically. If cost data exists, I would choose the threshold that maximises expected savings.
17
How do you choose a classification threshold that maximises F1?
from sklearn.metrics import precision_recall_curve
import numpy as np
proba = clf.predict_proba(X_val)[:, 1]
p, r, t = precision_recall_curve(y_val, proba)
f1 = 2 * p * r / (p + r + 1e-9)
best = t[np.argmax(f1[:-1])]
print(best)
Choose it on validation data, not the test set. scikit-learn 1.5+ also offers TunedThresholdClassifierCV.
18
How do you build a text classifier with TF-IDF and logistic regression?
Situation: our lead-scoring model had the best AUC with a large ensemble, but the CRM needed scores within 50 ms. Task: meet latency without losing much quality. Action: I compared a smaller LightGBM model with fewer features and measured both AUC and latency. Result: we lost 0.01 AUC but met the 50 ms limit, and sales teams adopted it because scores appeared instantly.
Softmax turns a vector of raw scores into probabilities that sum to one: p_i = exp(z_i) / sum of exp(z_j). It is used in multinomial logistic regression and the output layer of neural networks for multiclass problems. Larger scores get exponentially larger shares. In practice the maximum score is subtracted before exponentiation to avoid numerical overflow.
The kernel trick computes dot products of data in a high-dimensional feature space without explicitly transforming the data, using a kernel function such as RBF, exp(-gamma times squared distance). This lets linear algorithms like SVM learn non-linear boundaries efficiently. In RBF SVMs, larger gamma makes each point's influence more local, which can lead to overfitting.
22
What is probability calibration and why does it matter?
A calibrated model's predicted probabilities match real frequencies: of cases scored 0.8, about 80 percent should be positive. It matters when scores drive decisions like credit limits or expected loss. Random forests and SVMs are often poorly calibrated. Fix it with CalibratedClassifierCV using Platt scaling (sigmoid) or isotonic regression, and check with a reliability curve or Brier score.
23
Your classifier works well overall but poorly for one customer segment. What do you do?
I would measure metrics per segment to confirm, then check if that segment is under-represented or has different feature distributions. Fixes include collecting more data, sample weighting, segment-specific features, or a separate model if the behaviour is truly different. If the segment relates to a protected attribute, I would also review fairness metrics and involve the compliance team.
24
A credit model's probability of 0.3 is used to set interest rates, but defaults in that band are 12 percent. What is wrong?
The model is poorly calibrated: it ranks customers but its probabilities are not reliable. Since pricing depends on actual probabilities, I would calibrate it with isotonic regression or Platt scaling on a held-out set and validate with a reliability curve and Brier score. I would also check whether the default rate has drifted since training, which would require retraining.
Use an imbalanced-learn pipeline so oversampling happens only on training folds.
from imblearn.pipeline import Pipeline
from imblearn.over_sampling import SMOTE
from sklearn.model_selection import cross_val_score
pipe = Pipeline([('pre', pre), ('smote', SMOTE(random_state=42)),
('clf', LogisticRegression(max_iter=1000))])
print(cross_val_score(pipe, X, y, cv=5, scoring='average_precision').mean())
Applying SMOTE before splitting puts synthetic copies of test points into training.
Frequently asked questions
How important is Classification Models in Machine Learning interviews?
Classification predicts categories such as fraud, churn or spam. This topic covers logistic regression, k-NN, Naive Bayes and SVMs, the confusion matrix, precision, recall, F1, ROC-AUC and PR-AUC, class imbalance, threshold tuning, probability calibration and multiclass strategies, with practical scikit-learn code that interviewers often ask you to write.
How should I use the Classification Models sheet?
Go top to bottom — easy to hard. Answer before opening each answer, tick what you solved and save misses to Last-Minute Revision.