Free demoJob-oriented courses at Debugshala, Old Palasia, Indore. Freshers, non-IT and year-gap welcome.Book a free demo +91-8982385539

Machine Learning Interview Questions: 30 Answers with Code

Lesson 27 of 27 · 15 min · Updated 13 Nov 2026

30 machine learning interview questions with crisp answers and runnable code: bias-variance, leakage, regularisation, metrics, algorithms and scenarios.

At a two-wheeler workshop on the MR-10 road, the owner tests a new mechanic in a simple way. He rolls in a scooter that will not start and asks, "What do you check first, and why?" He is not looking for a recited manual. He wants a clear order of checks: fuel, spark plug, battery.

Machine learning interviews work the same way. The scooter is a model or dataset with a problem. The order of checks is your reasoning: what you would look at, what the numbers mean and what you would try next.

Machine learning interview questions test whether you can explain core ML concepts precisely, choose and evaluate models for a given problem, and diagnose what has gone wrong when a model underperforms.

This page is the revision sheet for the Machine Learning Tutorial series on this site, and each answer names the tutorial that covers it in depth.

Pair it with one portfolio piece from ML projects for beginners, because interviewers always ask about your own work. For statistics, SQL and pandas questions, see data science interview questions. Code runs on scikit-learn 1.7.

How to answer a machine learning interview question

Machine learning interview answer framework: four steps from a one-line definition, to intuition, to a concrete example with numbers, to trade-offs and when not to use the method

A four-part answer: define it, explain why it works, show a number, then say when it fails. Most answers need only 30–60 seconds.

Interviewers usually ask a follow-up to whatever you say. Ending with a trade-off ("this works well unless the classes are imbalanced") invites the follow-up you are ready for.

Machine learning fundamentals interview questions

1. What is the difference between supervised, unsupervised and reinforcement learning?

Supervised learning trains on labelled examples to predict a known target, such as loan default. Unsupervised learning finds structure in unlabelled data, such as customer segments. Reinforcement learning learns actions by trial and error from rewards, such as a robot learning to balance. The AI vs ML vs deep learning tutorial compares all three.

2. What is the difference between classification and regression?

Classification predicts a category (spam or not, which of five products). Regression predicts a number (house price, delivery time). Logistic regression, despite its name, is a classification model.

3. What is the difference between parameters and hyperparameters?

Parameters are learned from data during training, such as regression coefficients or neural network weights. Hyperparameters are set before training and control how learning happens, such as tree depth, k in KNN or the learning rate. You choose hyperparameters with validation data, as in the hyperparameter tuning tutorial.

4. Explain the bias-variance trade-off.

Bias is error from a model too simple to capture the pattern (underfitting). Variance is error from a model so flexible that it fits noise and changes a lot with different training samples (overfitting). Making a model more complex lowers bias but raises variance; the best model balances the two. See overfitting and the bias-variance trade-off.

5. How do you tell whether a model is overfitting or underfitting?

Compare training and validation scores. Both poor means underfitting. Training excellent but validation much worse means overfitting.

Machine learning model diagnosis flowchart: if training error is high the model underfits, so add features or use a more complex model; if training error is low but validation error is high it overfits, so add data, regularise or simplify; if both are low the model is ready to test

Read the training error first, then the gap to validation error. Each case has its own set of fixes.

6. Why do we need separate training, validation and test sets?

The training set fits the model, the validation set compares models and tunes hyperparameters, and the test set gives one final, unbiased estimate. If you tune on the test set, its score becomes optimistic because you have indirectly fitted to it. A common split is 70/15/15, or cross-validation on the training part plus one test set.

7. What is k-fold cross-validation, and when do you use stratified or time-series splits?

K-fold splits data into k folds, trains on k−1 and validates on the remaining one, rotating k times and averaging the scores. Use stratified folds for classification so each fold keeps the class ratio, and time-series splits for dated data so you never train on the future. Full detail is in cross-validation.

Quick check

Training accuracy 65%, validation accuracy 63%. What is the most likely problem?

Data and feature engineering interview questions

8. What is data leakage? Give an example.

Data leakage is when information that would not be available at prediction time gets into training, making validation scores unrealistically high. A classic example is a feature recorded after the outcome, such as "number of recovery calls made" in a loan-default model.

Python
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score

rng = np.random.default_rng(0)
n = 1000
income = rng.normal(50, 15, n)
emi_ratio = rng.uniform(0.1, 0.7, n)
default = (rng.random(n) < 1 / (1 + np.exp(-(-4 + 6 * emi_ratio - 0.02 * (income - 50))))).astype(int)
recovery_calls = default * rng.poisson(6, n)          # only exists AFTER a default happens

honest = np.column_stack([income, emi_ratio])
leaky = np.column_stack([income, emi_ratio, recovery_calls])
for name, X in [("honest features", honest), ("with leaked feature", leaky)]:
    print(f"{name:>20}: CV accuracy {cross_val_score(LogisticRegression(), X, default, cv=5).mean():.3f}")

The leaked feature pushes accuracy close to perfect, but in production the column would always be zero for new applicants. Another common leak is fitting a scaler or imputer on all data before splitting; a Pipeline prevents it.

9. Which models need feature scaling, and which do not?

Distance-based and gradient-based models need scaling: KNN, SVM, k-means, PCA, regularised linear models and neural networks. Tree-based models (decision trees, random forests, gradient boosting) do not, because splits depend only on the order of values.

Python
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import cross_val_score

rng = np.random.default_rng(1)
n = 600
age = rng.uniform(21, 60, n)                     # informative, small scale
salary = rng.normal(600000, 200000, n)           # pure noise, huge scale (rupees)
y = (age > 40).astype(int)
X = np.column_stack([age, salary])

raw = cross_val_score(KNeighborsClassifier(), X, y, cv=5).mean()
scaled = cross_val_score(make_pipeline(StandardScaler(), KNeighborsClassifier()), X, y, cv=5).mean()
print(f"KNN without scaling: {raw:.3f}")
print(f"KNN with scaling:    {scaled:.3f}")

Unscaled, the noisy salary column dominates every distance and KNN is near a coin toss. Methods are compared in the data preprocessing tutorial on scaling and encoding.

10. How do you handle missing values?

First ask why they are missing. Then drop columns that are mostly empty, impute numbers with the median and categories with the mode or an "Unknown" label, and consider a "was missing" indicator column, since missingness itself can be informative. Fit imputers on training data only. The handling missing data tutorial covers each option.

11. How do you encode categorical variables?

Use one-hot encoding for nominal categories with few levels (city, payment mode), ordinal encoding when there is a natural order (small < medium < large), and target or frequency encoding for high-cardinality columns such as PIN codes. Target encoding must be done inside cross-validation, or it leaks the label.

12. How do you handle an imbalanced dataset?

Start by choosing the right metric (precision, recall, F1 or PR-AUC instead of accuracy). Then try class weights, resampling such as SMOTE on training folds only, and tuning the decision threshold. The full toolkit is in handling imbalanced data.

13. What is the curse of dimensionality, and how do you deal with it?

As features grow, data becomes sparse and distances between points become similar, so distance-based models and density estimates degrade and more data is needed. Remedies are feature selection, dimensionality reduction such as PCA, regularisation, and choosing models that cope well with many features.

Quick check

Which model does NOT need feature scaling?

Machine learning algorithm interview questions

14. What are the assumptions of linear regression?

A linear relationship between features and target, independent errors, constant error variance (homoscedasticity), roughly normal errors for valid confidence intervals, and no strong multicollinearity. Check them with residual plots. The linear regression tutorial shows how to read residuals.

15. Why is logistic regression used for classification?

It passes a linear score through the sigmoid function, producing a probability between 0 and 1, and is trained with log loss. A threshold, 0.5 by default, turns the probability into a class. Its coefficients are log-odds, which makes it easy to explain. More in the logistic regression tutorial.

16. What is the difference between L1 and L2 regularisation?

Both add a penalty on coefficient size to reduce overfitting. L2 (ridge) penalises squared coefficients and shrinks them all smoothly. L1 (lasso) penalises absolute values and can push some coefficients exactly to zero, so it also selects features.

Python
import numpy as np
from sklearn.linear_model import Ridge, Lasso
from sklearn.preprocessing import StandardScaler

rng = np.random.default_rng(2)
X = StandardScaler().fit_transform(rng.normal(0, 1, (200, 8)))
y = 3 * X[:, 0] - 2 * X[:, 1] + rng.normal(0, 1, 200)   # only 2 of 8 features matter

for model in [Ridge(alpha=10), Lasso(alpha=0.2)]:
    coef = model.fit(X, y).coef_ + 0.0          # + 0.0 turns -0.0 into 0.0 for printing
    print(f"{type(model).__name__:>5}: {np.round(coef, 2)} | exact zeros: {(coef == 0).sum()}")

Lasso sets the six useless coefficients to exactly zero; ridge only makes them small. More in ridge, lasso and elastic net.

17. How does a decision tree choose where to split?

At each node it tries candidate splits and picks the one that most reduces impurity, measured by Gini impurity or entropy for classification and by squared error for regression. Gini for a node is 1 − Σ pᵢ², so a pure node scores 0.

Python
def gini(counts):
    total = sum(counts)
    return 1 - sum((c / total) ** 2 for c in counts)

parent = [50, 50]                      # 50 repaid, 50 defaulted
left, right = [40, 10], [10, 40]       # split on "EMI ratio < 0.4"
weighted = (sum(left) * gini(left) + sum(right) * gini(right)) / sum(parent)
print(f"parent Gini {gini(parent):.2f}, after split {weighted:.2f}, gain {gini(parent) - weighted:.2f}")

The split cuts impurity from 0.50 to 0.32. See decision trees explained.

18. What is the difference between bagging and boosting?

Bagging trains many models independently on bootstrap samples and averages them, which mainly reduces variance; random forest is the best-known example. Boosting trains models one after another, each correcting the errors of the previous ones, which mainly reduces bias; gradient boosting and XGBoost are examples. See ensemble learning and the random forest and gradient boosting tutorials.

19. How does an SVM work, and what is the kernel trick?

A support vector machine finds the boundary with the widest margin between classes; the closest points, the support vectors, define it.

The kernel trick computes similarities as if the data were mapped into a higher-dimensional space, letting a linear boundary there become a curved one in the original space, without building that space explicitly. The SVM tutorial in this series walks through margins and kernels.

20. How does KNN work, and how do you choose k?

KNN stores the training data and predicts by a vote (or average) of the k closest points. Small k overfits to noise; large k blurs class boundaries. Choose k by cross-validation, usually an odd number for binary problems, and always scale features first. More in the KNN tutorial.

21. Why is Naive Bayes called "naive"?

It assumes all features are independent given the class, which is rarely true. Even so, it works well for text classification such as spam filtering, because the ranking of class probabilities is often right even when the probabilities themselves are off. It is fast and needs little data. More in the Naive Bayes tutorial.

22. How does k-means work, and how do you choose the number of clusters?

K-means places k centroids, assigns each point to the nearest one, moves each centroid to the mean of its points, and repeats until assignments stop changing. Choose k using the elbow in inertia, the silhouette score and, most importantly, whether the clusters make business sense. The k-means tutorial shows both methods.

Model evaluation interview questions

23. Explain precision, recall and F1 with an example.

Precision is the share of predicted positives that are correct; recall is the share of actual positives that were found; F1 is their harmonic mean. Prefer recall when missing a positive is costly (cancer screening) and precision when false alarms are costly (blocking a customer's card).

Python
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score

# 20 transactions: 1 = fraud. The model flags 6, of which 4 are real frauds.
y_true = [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
y_pred = [1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
print(f"TP={tp} FP={fp} FN={fn} TN={tn}")
print(f"precision = {tp}/({tp}+{fp}) = {precision_score(y_true, y_pred):.2f}")
print(f"recall    = {tp}/({tp}+{fn}) = {recall_score(y_true, y_pred):.2f}")
print(f"F1        = {f1_score(y_true, y_pred):.2f}")

Being able to compute these by hand from a confusion matrix is a frequent screening question. See model evaluation metrics.

24. When is accuracy a misleading metric?

When classes are imbalanced. With 1% fraud, a model that predicts "not fraud" for everyone is 99% accurate and useless. Use precision, recall, F1, PR-AUC or a cost-based metric instead.

25. What is the difference between ROC-AUC and PR-AUC?

ROC-AUC measures how well the model ranks positives above negatives across all thresholds, plotting true positive rate against false positive rate. PR-AUC plots precision against recall and focuses on the positive class. On heavily imbalanced data, ROC-AUC can look high while precision is poor, so PR-AUC is more informative there.

26. How do you choose the classification threshold?

Do not assume 0.5. Choose the threshold on validation data by the business cost: for example, the lowest threshold that keeps precision above 80%, or the one that minimises (cost of a miss × misses) + (cost of a false alarm × false alarms). Then fix it before scoring the test set.

27. MAE, RMSE or R² for regression: which would you report?

MAE is the average absolute error in the target's units and is robust to outliers. RMSE squares errors first, so it punishes large misses more. R² is the share of variance explained, useful for comparison but unitless. For a business audience, "off by ₹1,800 on average" (MAE) is the clearest.

Quick check

A cancer screening model must catch as many real cases as possible. Which metric matters most?

Scenario-based machine learning interview questions

28. Your model has 98% training accuracy but 72% test accuracy. What do you do?

That gap signals overfitting. Check first for leakage or a train/test distribution mismatch. Then simplify the model (shallower trees, fewer features), add regularisation, gather more data or use augmentation, and confirm each change with cross-validation, not the test set.

29. A model that worked well at launch has degraded after three months. Why?

The likely cause is drift. Data drift means the input distribution changed (new customer segments, a festival season); concept drift means the relationship between inputs and target changed (fraudsters adapt). Monitor input distributions and live metrics, retrain on recent data and set alerts. See ML monitoring and drift.

30. How would you choose an algorithm for a new tabular problem?

Start with a quick baseline (a mean predictor or logistic regression) to set the bar. Then try a tree ensemble such as random forest or gradient boosting, which usually does best on tabular data. Weigh accuracy against interpretability, latency and maintenance, and pick the simplest model that meets the business target.

Common mistakes in machine learning interviews

  • Reciting definitions without an example. Add one number or one real use case to every answer.
  • Saying "I'd use deep learning" for small tabular data. Interviewers expect a baseline and tree models first.
  • Quoting accuracy on imbalanced data. Name the metric that fits the cost of errors.
  • Not knowing your own project. Be ready to explain the data, the metric, the baseline and what failed.
  • Bluffing. "I haven't used that, but here is how I'd reason about it" scores better than a wrong answer.

Mock interviews with honest feedback make the biggest difference, and Debugshala's ML course in Indore, which offers placement assistance, is one place to get that practice. For timed revision, take ML interview-style MCQs on Debugshala Test. If you also want the wider data science syllabus, see the full Data Science & AI programme at Debugshala.

Key takeaways

  • Structure each answer: definition, intuition, example with numbers, trade-off.
  • Fundamentals (bias-variance, splits, cross-validation, leakage) come up in almost every round.
  • Know which models need scaling and why, and how L1, L2, trees, SVMs and KNN work.
  • Pick evaluation metrics by the cost of errors, and choose thresholds on validation data.
  • Scenario questions reward a calm order of checks: leakage, distribution shift, then model fixes.
Take the free Machine Learning quiz10 quick questions with explanations. No sign-up needed, and you can come back to this lesson after.

FAQ

What questions are asked in a machine learning interview for freshers?

Freshers are usually asked to explain supervised vs unsupervised learning, overfitting and the bias-variance trade-off, train/validation/test splits, cross-validation, precision vs recall, and how algorithms such as linear regression, logistic regression, decision trees, random forests and k-means work. Expect at least one question about your own project.

How should I prepare for a machine learning interview?

Revise core concepts until you can explain each in under a minute with an example. Practise coding small models in scikit-learn, compute metrics by hand from a confusion matrix, and rehearse a two-minute walkthrough of one end-to-end project, including what went wrong and how you fixed it.

Do machine learning interviews include coding rounds?

Most do. Expect Python and pandas data manipulation, sometimes SQL, and occasionally implementing a simple algorithm such as k-means or gradient descent in NumPy. Some companies also give a take-home dataset where you build and explain a model within a day or two.

Which machine learning algorithms are asked most in interviews?

Linear and logistic regression, decision trees, random forest, gradient boosting, k-nearest neighbours, Naive Bayes, SVM, k-means and PCA appear most often. Interviewers care less about formulas and more about when you would use each one and what its weaknesses are.

Does Debugshala help with machine learning interview preparation in Indore?

Debugshala's Machine Learning course at the Old Palasia campus in Indore comes with placement assistance, and classes are taught in English and Hindi. Fresh graduates, non-IT learners and people with a year gap can join, and you can attend a free demo class before paying anything.

From the team behind this site

Go further with the Data Science & AI Course at Debugshala

These tutorials are written by Debugshala, a training and placement institute at Old Palasia, Indore. The classroom course adds live mentoring, real projects and interview preparation.

  • Offline classes at Old Palasia, Indore
  • Free demo class before you pay
  • Non-IT, fresher and year-gap friendly
  • Placement assistance

700+ students placed · 200+ hiring partners · 4.9★ Google rating

Machine Learning Projects for Beginners (with a Portfolio Plan)