Machine Learning Interview Questions and Answers: 60 Questions from Easy to Hard
This guide is for freshers in India preparing for their first data science or machine learning interview, and for analysts and engineers with a few years of experience moving into ML roles. It has 60 questions with model answers in three levels: the basics every fresher is asked, then the practical questions on metrics, validation, leakage and tuning that come up for 1-4 years of experience, and finally senior questions on model internals, deployment, drift and explaining models to business teams. The worked numbers in the answers are small enough to check by hand. Try each answer aloud before reading ours, then take the mock test to practise under time pressure.
- 60 questions, 3 levels
- Updated
- CoreHRx Jobs
- 1,217 open Machine Learning jobs
What machine learning interviews cover
- Core ideas: supervised versus unsupervised learning, classification versus regression, train, validation and test splits, and the bias-variance trade-off behind overfitting.
- Algorithms: linear and logistic regression, decision trees, random forests, gradient boosting, SVM, KNN, k-means and PCA, with how each one learns and when it fails.
- Evaluation: precision, recall, F1, ROC-AUC, PR-AUC, RMSE and R-squared, cross-validation, and choosing the metric that matches the business cost of each kind of error.
- Practical data work: missing values, encoding, scaling, feature engineering, imbalanced classes and data leakage, often tested through a scenario or a take-home assignment.
- For experienced roles: hyperparameter tuning, deployment, monitoring for drift, probability calibration and explaining a model to business and compliance teams.
How to prepare (freshers and experienced)
- Freshers: explain each common algorithm in plain words and state one strength and one weakness. Interviewers value clear intuition over memorised formulas.
- Work the basics by hand at least once: a confusion matrix, precision and recall, Gini impurity, one gradient descent step and RMSE. Doing this calmly in the room stands out.
- Build one end-to-end project on a real Indian dataset, such as crop prices, loan approvals or ride demand, with a scikit-learn pipeline, cross-validation and a short note on what you would try next.
- Experienced candidates: prepare two or three stories from your own work covering the business problem, the metric you chose, a mistake such as leakage that you caught, and what happened after deployment.
- Revise the scikit-learn pieces you use daily (Pipeline, ColumnTransformer, GridSearchCV, cross_val_score) and know which defaults changed in recent versions, such as n_init in KMeans from 1.4.
Common mistakes and interview-day tips
- Quoting accuracy on imbalanced data. Ask what the class balance is and which mistake costs more before you name a metric.
- Preprocessing the full dataset before splitting it. Fitting a scaler, imputer or encoder on all rows leaks test information; say that you put these steps inside a pipeline.
- Jumping to deep learning for a small tabular problem. Start with a simple baseline, then a tree ensemble, and justify any extra complexity with validation numbers.
- Giving textbook definitions without an example. Tie each answer to a concrete case, such as fraud detection for a UPI app or demand forecasting for a grocery delivery firm.
- Stopping at I do not know. Say how you would find out instead: which plot you would draw, which experiment you would run or which metric you would check.
Machine Learning interview questions: Easy questions (freshers)
20 questions1. What is machine learning, and how is it different from traditional programming?
In traditional programming a developer writes the rules and the computer applies them to data. In machine learning we give the computer examples, inputs with known outputs, and an algorithm learns the rules, the model, from them. For example, instead of hand-writing rules to flag a risky loan, we train a model on past applications labelled repaid or defaulted. ML suits problems where the rules are too many or keep changing, such as spam or fraud. It needs enough good data, and the model is only as good as that data.
2. What is the difference between supervised and unsupervised learning?
In supervised learning every training row has a label, the answer we want to predict, such as a house price or whether a customer will churn, and the model learns the mapping from features to label. In unsupervised learning there are no labels; the algorithm finds structure on its own, such as customer segments with k-means or a compressed view of the data with PCA. Semi-supervised learning mixes a few labelled rows with many unlabelled ones, and reinforcement learning learns from rewards instead of labels.
3. How do you decide whether a problem is classification or regression?
Look at the target. If it is a category, it is classification: spam or not spam, which of five product types, will a customer churn. If it is a continuous number, it is regression: delivery time in minutes or next month's sales in rupees. The choice decides the loss and the metrics, for example log loss and F1 for classification and RMSE for regression. A trap: a numeric code such as a pin code is not a quantity at all, so ask whether the order and gaps between values really mean something.
4. Why do we split data into training, validation and test sets?
The model learns from the training set, we compare models and tune settings on the validation set, and the test set is used once at the end to estimate how the final model will do on new data. 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 a train and test split with cross-validation inside the training part. For time-based data such as daily sales, split by date so the model never trains on the future.
5. What are overfitting and underfitting, and how do you spot them?
Overfitting means the model has memorised noise in the training data: it scores very well on training data but much worse on validation data, say 99% against 78%. Underfitting means the model is too simple to capture the pattern, so both scores are poor. You spot them by comparing training and validation scores, or with a learning curve. Fixes for overfitting: more data, a simpler model, regularization or early stopping. Fixes for underfitting: more useful features or a more flexible model.
6. How does simple linear regression work? Fit the line for this delivery data.
Linear regression fits y = b0 + b1 x by choosing the intercept b0 and slope b1 that minimise the sum of squared errors (ordinary least squares). The slope is the covariance of x and y divided by the variance of x. Here the mean distance is 6 km and the mean time 32 minutes, which gives a slope of 3.55 and an intercept of 10.7: each extra km adds about 3.55 minutes, so a 7 km order is predicted at 35.5 minutes. It assumes a roughly straight-line relationship, so plot the data and the residuals first.
distance_km minutes
2 18
4 25
6 31
8 40
10 467. Logistic regression has regression in its name. Why is it used for classification?
It is a linear model whose output goes through the sigmoid function, 1 / (1 + e^-z), which turns any score z into a probability between 0 and 1. The model predicts the log-odds of the positive class as a linear function of the features, and a threshold, 0.5 by default, turns the probability into a class. For example z = 0 gives 0.5, z = 2 gives 0.881 and z = -1 gives 0.269. It is trained by minimising log loss, not squared error. It is fast, easy to explain and a strong baseline for binary problems.
8. What is a confusion matrix, and why can accuracy be misleading?
A confusion matrix counts the four outcomes of a binary classifier: true positives, false positives, false negatives and true negatives. Accuracy is (TP + TN) divided by all predictions. On imbalanced data it misleads: if 50 of 10,000 UPI transactions are fraud, a model that calls everything genuine scores 99.5% accuracy and catches zero fraud. That is why you read the matrix itself and the precision and recall of the class you care about. Always state the class balance next to any accuracy figure.
9. Explain precision and recall using this confusion matrix for a loan default model.
Precision asks: of the loans we flagged as default, how many really defaulted? 40 / (40 + 10) = 0.80. Recall asks: of the real defaulters, how many did we catch? 40 / (40 + 20) = 0.667. F1, their harmonic mean, is 0.727, while accuracy is 0.97, which hides the 20 missed defaulters. Favour recall when missing a positive is costly, as in fraud or disease screening; favour precision when a false alarm is costly, such as blocking a good customer. Moving the threshold trades one for the other.
predicted default predicted repay
actual default 40 (TP) 20 (FN)
actual repay 10 (FP) 930 (TN)10. How does a decision tree choose a split? Explain with Gini impurity.
At each node the tree tries candidate splits on every feature and picks the one that makes the child nodes purest. Gini impurity is 1 minus the sum of squared class shares. A node with 40 buyers and 60 non-buyers has Gini 1 - 0.4^2 - 0.6^2 = 0.48. If a split sends 30 buyers and 10 non-buyers left and 10 and 50 right, the children have Gini 0.375 and 0.278, weighted 0.317, a drop of 0.163. The tree keeps splitting until a stopping rule such as max_depth, which is also how you control overfitting.
11. How does KNN make a prediction, and why must you scale features for it?
K-nearest neighbours stores the training data and, for a new point, finds the k closest rows by a distance such as Euclidean, then takes a majority vote for classification or the average for regression. There is no real training step, so prediction is slow on big data. Distance is dominated by the feature with the largest range: here d(A, B) is 100.1 and d(A, C) is 1000.0, so spend decides everything and age is ignored. Scale the features first, and choose k with cross-validation.
customer age monthly_spend_rs
A 25 2000
B 30 2100
C 26 300012. How does k-means clustering work?
You choose k, the number of clusters. The algorithm places k centroids, assigns each point to its nearest centroid, moves each centroid to the mean of its points, and repeats until the assignments stop changing. It minimises the within-cluster sum of squared distances, called inertia. A retail chain might use it to segment customers by spend and visit frequency. Weaknesses: you must pick k, the result depends on the starting centroids (scikit-learn uses k-means++ to start well), it assumes round clusters and it needs scaled features.
13. What is the difference between standardization and min-max scaling, and when do you need either?
Standardization subtracts the mean and divides by the standard deviation, giving mean 0 and standard deviation 1: for 10, 20, 30, 40, 50 the mean is 30, the standard deviation 14.14, and 50 becomes 1.41. Min-max scaling maps the range to 0 to 1, so 40 becomes 0.75. Distance and gradient based models (KNN, SVM, k-means, PCA, regularized linear models, neural networks) need scaling; tree models do not. Fit the scaler on training data only and reuse it on the test data, as in the snippet.
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_s = scaler.fit_transform(X_train) # learns mean and std
X_test_s = scaler.transform(X_test) # reuses them14. How do you encode categorical features such as city or payment mode?
For categories with no order, such as city (Pune, Chennai, Jaipur) or payment mode (UPI, card, cash), use one-hot encoding: one 0/1 column per category. Ordinal encoding (0, 1, 2) suits only ordered values such as small, medium and large; applied to cities it invents a false order that linear models and KNN will use. Set handle_unknown to ignore so that a new city in production does not crash the model. Very high-cardinality columns such as pin codes need other methods, such as target or frequency encoding.
from sklearn.preprocessing import OneHotEncoder
enc = OneHotEncoder(handle_unknown="ignore", sparse_output=False)
X_city = enc.fit_transform(df[["city"]])15. How do you handle missing values in a dataset?
First find out why values are missing: a blank income field may mean the applicant skipped it, which can itself be predictive. Options are dropping rows (only if they are few and missing at random), dropping a mostly empty column, or imputing: the median for skewed numbers, the most frequent value or a separate Missing category for categories. A was-missing indicator column keeps the signal. Fit the imputer on training data only. Some models, such as HistGradientBoosting in scikit-learn, handle missing values natively.
from sklearn.impute import SimpleImputer
imp = SimpleImputer(strategy="median", add_indicator=True)
X_train_i = imp.fit_transform(X_train)
X_test_i = imp.transform(X_test)16. What is gradient descent? Show one update step.
Gradient descent minimises a loss by repeatedly moving the parameters a small step against the gradient, the direction in which the loss rises fastest. With w = 1 and a learning rate of 0.05, the gradient is 2 x (2 - 10) x 2 = -32, so w becomes 1 + 0.05 x 32 = 2.6, and the loss falls from 64 to 23.04 (the best w is 5). The learning rate matters: too small and training crawls, too large and the loss jumps around or diverges. Mini-batch gradient descent updates on small batches of rows.
loss(w) = (w * x - y) ** 2 with x = 2, y = 10
gradient = 2 * (w * x - y) * x
w_new = w - learning_rate * gradient17. What are MAE, MSE and RMSE, and how do they differ?
MAE is the average absolute error, MSE the average squared error and RMSE its square root, in the same unit as the target. For these four days of demand in units sold, MAE is 13.75, MSE is 281.25 and RMSE is 16.77. RMSE is above MAE because squaring makes the single 30-unit miss count far more. Use RMSE when large errors are especially bad, and MAE when every unit of error costs the same or outliers should not dominate. In scikit-learn 1.4 and later, root_mean_squared_error gives RMSE directly.
actual 120 150 90 200
predicted 110 160 95 170
error -10 10 5 -3018. What do bias and variance mean in machine learning?
Bias is error from wrong assumptions: a straight line fitted to a curved pattern misses it whatever sample of data you train on. Variance is error from sensitivity to the particular training sample: a deep tree changes a lot if you retrain it on slightly different rows. High bias leads to underfitting and high variance to overfitting. Making a model more flexible usually lowers bias and raises variance, so we look for the point with the lowest validation error, using regularization, more data or ensembles to control variance.
19. What is k-fold cross-validation, and why is it better than a single train-test split?
The data is split into k folds; the model trains on k - 1 folds and is scored on the remaining one, k times, so every row is used for validation once. You report the mean and the spread: fold scores of 0.81, 0.84, 0.79, 0.83 and 0.80 give a mean of 0.814 with a standard deviation of about 0.019. One split can be lucky or unlucky; k-fold gives a steadier estimate and shows how stable the model is. For classifiers, cv=5 in scikit-learn uses stratified folds automatically.
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X, y, cv=5, scoring="f1")
print(scores.mean(), scores.std())20. What does a single neuron in a neural network compute?
A neuron multiplies each input by a weight, adds a bias, and passes the sum z through an activation function. Here z = 0.4 - 0.08 + 0.1 = 0.42; ReLU, max(0, z), outputs 0.42 and sigmoid outputs 0.603. Layers of neurons stacked together form a network, and training adjusts the weights with backpropagation and gradient descent. The activation must be non-linear: without it, any number of layers collapses into one linear model and cannot learn curved patterns.
inputs = [0.5, 0.2]
weights = [0.8, -0.4]
bias = 0.1
z = 0.5 * 0.8 + 0.2 * (-0.4) + 0.1 # 0.42Ready to check yourself? Take the free Machine Learning mock test
Machine Learning interview questions: Medium questions (1-3 years)
20 questions21. What is the difference between L1 (Lasso) and L2 (Ridge) regularization?
Both add a penalty on the size of the coefficients to the loss, so the model cannot overfit with huge weights. L2 adds the sum of squared coefficients: it shrinks all of them smoothly but rarely to exactly zero, and copes well with correlated features. L1 adds the sum of absolute values: it pushes many coefficients to exactly zero, so it also selects features. In scikit-learn, alpha sets the strength for Ridge and Lasso, while in LogisticRegression C is the inverse. Scale the features first, or the penalty hits them unevenly.
from sklearn.linear_model import Lasso, Ridge
ridge = Ridge(alpha=1.0).fit(X_train_s, y_train)
lasso = Lasso(alpha=0.1).fit(X_train_s, y_train)
print((lasso.coef_ == 0).sum(), "features dropped by L1")22. How does a random forest reduce overfitting compared with a single decision tree?
A random forest trains many deep trees, each on a bootstrap sample of the rows and, at each split, on a random subset of features (max_features='sqrt' by default for classification, so 10 of 100 features). Averaging trees that make different errors cuts variance while keeping the low bias of deep trees. Each bootstrap sample leaves out about 36.8% of the rows, which gives a free out-of-bag score with oob_score=True. Trade-offs: a larger model, slower prediction and less interpretability than one tree.
23. How is gradient boosting different from a random forest?
A random forest builds independent deep trees and averages them to cut variance. Gradient boosting builds shallow trees one after another, each fitted to the errors (the negative gradient of the loss) of the ensemble so far, and adds it scaled by a learning rate. It mainly cuts bias and is often the most accurate choice on tabular data, but it can overfit with too many trees, so tune the learning rate and the number of trees together and use early stopping. In scikit-learn, HistGradientBoosting is the fast choice for large data.
24. Explain how an SVM works, including the C parameter and kernels.
A linear SVM finds the boundary with the widest margin between the classes; the margin is 2 / ||w||, so for w = (3, 4) it is 0.4. Only the closest points, the support vectors, define it. C trades margin width against training errors: a small C allows more violations and a smoother boundary, a large C fits the training data tightly and can overfit. Kernels such as RBF compare points through a similarity function, which gives non-linear boundaries without building new features by hand. SVMs need scaled features.
25. What does ROC-AUC measure? Work it out for this small set of scores.
The ROC curve plots the true positive rate against the false positive rate as the threshold moves, and AUC is the area under it. It equals the chance that a random positive gets a higher score than a random negative. Here there are 4 x 6 = 24 positive-negative pairs and 21 are ordered correctly (0.55 is below 0.70, and 0.40 is below 0.70 and 0.50), so AUC = 0.875. It does not depend on any threshold. On heavily imbalanced data also check PR-AUC, which reflects precision on the rare class.
positives: 0.90 0.80 0.55 0.40
negatives: 0.70 0.50 0.35 0.30 0.20 0.1026. How would you pick the classification threshold for a loan default model?
Do not keep 0.5 by habit; choose the threshold that matches the business cost. Here the total cost is Rs 8,40,000 at 0.5, Rs 5,50,000 at 0.3 and Rs 6,00,000 at 0.2, so 0.3 wins, with recall 0.80 and precision 0.35. Get the costs from the credit team, pick the threshold on validation data rather than the test set, and recheck it when default rates or costs change. If the bank can review only 200 cases a day, that capacity can set the threshold instead.
threshold TP FP FN (100 real defaulters)
0.5 60 40 40
0.3 80 150 20
0.2 90 400 10
cost: FN = Rs 20,000 (bad loan), FP = Rs 1,000 (lost customer)27. What is data leakage? Give examples you would check for.
Leakage is when training data contains information that will not be available at prediction time, so validation scores look great and production fails. Common cases: fitting a scaler or imputer on the full data before splitting; a feature recorded after the outcome, such as the number of collection calls in a default model; the same customer in both train and test; random splits on time-ordered data. Warning signs are a sudden jump in score or one feature dominating. Use pipelines, split by time or group, and ask when each column is filled.
28. Your fraud dataset has 9,900 genuine and 100 fraud rows. How do you train on it?
First choose a metric that reflects the rare class: recall, precision, PR-AUC or cost, never plain accuracy. Then make mistakes on fraud count more: class_weight='balanced' gives each fraud row a weight of 50 and each genuine row 0.505, so both classes carry equal total weight. Alternatives are undersampling the majority or oversampling such as SMOTE, but only inside the training folds, never before the split. Use stratified splits so that every fold has fraud cases, and tune the threshold afterwards.
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(class_weight="balanced", max_iter=1000)
# weight = n_samples / (n_classes * rows in the class)
# genuine: 10000 / (2 * 9900) = 0.505
# fraud: 10000 / (2 * 100) = 5029. When would you use stratified k-fold, group k-fold or a time-series split?
Stratified k-fold keeps the class ratio in every fold: with 1,000 rows and 50 positives, each of 5 folds gets 10, so no fold is left without positives. Group k-fold keeps all rows of one group, such as a customer or a store, in the same fold, so the model is not rewarded for recognising a customer it saw in training. A time-series split always trains on earlier periods and tests on later ones, which matches demand or price forecasting. Plain shuffled k-fold on such data gives over-optimistic scores.
from sklearn.model_selection import (
GroupKFold, StratifiedKFold, TimeSeriesSplit)
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
cv = GroupKFold(n_splits=5) # pass groups=customer_id
cv = TimeSeriesSplit(n_splits=5) # past trains, future tests30. How do you tune hyperparameters with GridSearchCV, and what are the pitfalls?
GridSearchCV tries every combination and scores each with cross-validation: here 4 values of C times 3 class weights is 12 combinations, 60 fits with 5 folds, plus one final refit on all the training data. Put preprocessing in the pipeline so the scaler is refitted inside each fold, and name grid keys as step__param. Pitfalls: grids explode quickly (prefer RandomizedSearchCV for many parameters), best_score_ is optimistic because it was used for selection, and the test set must stay out of the search.
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
pipe = Pipeline([("scale", StandardScaler()),
("clf", LogisticRegression(max_iter=1000))])
grid = {"clf__C": [0.01, 0.1, 1, 10],
"clf__class_weight": [None, "balanced", {0: 1, 1: 10}]}
search = GridSearchCV(pipe, grid, cv=5, scoring="roc_auc")
search.fit(X_train, y_train)
print(search.best_params_, search.best_score_)31. What does PCA do, and how do you choose the number of components?
PCA finds new axes, combinations of the original features, ordered by how much variance they capture; the components are eigenvectors of the covariance matrix. For two features with covariance [[4, 2], [2, 3]] the eigenvalues are 5.56 and 1.44, so the first component explains 79.5% of the variance. Choose the number by cumulative explained variance, which a float n_components does, or by the downstream model score. Standardize first, or the feature with the largest unit dominates. Components are hard to explain to business users.
from sklearn.decomposition import PCA
pca = PCA(n_components=0.95) # keep 95% of the variance
Z = pca.fit_transform(X_train_s)
print(pca.n_components_, pca.explained_variance_ratio_)32. Your model scores 0.99 on training data and 0.78 on validation. What do you do?
That gap means high variance, or overfitting. I would first confirm the split is sound, with train and validation from the same population and period, so the gap is not a distribution shift. Then I would plot a learning curve: if the validation score keeps rising with more rows, more data will help. Otherwise reduce complexity: limit max_depth or raise min_samples_leaf for trees, increase regularization for linear models, drop noisy features, or use early stopping for boosting. Low, close scores would mean high bias instead.
33. Which features would you engineer to predict late deliveries for a quick-commerce app in Mumbai?
I would start from how a delay happens. From timestamps: hour of day, weekday, festival and monsoon flags. From geography: distance from the dark store to the customer and the area's past average delay. From load: orders in the last 30 minutes per active rider. From the order: number of items and whether it has heavy goods. Rolling averages must use only data from before the order time, or they leak the future. Then I would test each feature's value with cross-validated scores and permutation importance, not intuition.
34. What is multicollinearity, and why does it matter in linear regression?
Multicollinearity means two or more features are strongly correlated, such as floor area and number of rooms. Predictions may still be fine, but coefficients become unstable: signs can flip and standard errors grow, so you cannot trust them for explanation. Detect it with a correlation matrix or the variance inflation factor, VIF = 1 / (1 - R^2), where R^2 comes from regressing one feature on the others; an R^2 of 0.9 gives a VIF of 10, a common warning level. Fixes: drop or combine features, or use ridge.
35. How do you interpret a logistic regression coefficient?
A coefficient is the change in the log-odds of the positive class for a one-unit increase in that feature, with the others fixed. Exponentiating gives an odds ratio: a coefficient of 0.7 on has_previous_default means e^0.7 = 2.01, so the odds of default roughly double. The effect on probability depends on the starting point: log-odds of -2 is a probability of 0.119, and adding 0.7 gives -1.3, a probability of 0.214. Compare coefficients only on standardized features, and remember they show association, not cause.
36. Compute the information gain of this split using entropy.
Entropy is minus the sum of p log2 p over the classes. The parent, split 50/50, has entropy 1.0. Each child, split 75/25, has entropy 0.811, and as each child holds half the rows the weighted child entropy is also 0.811. Information gain is 1.0 - 0.811 = 0.189, and the tree picks the split with the highest gain. In scikit-learn criterion='entropy' uses this, and the default 'gini' gives very similar trees. A feature with many distinct values offers many candidate splits and can win by chance, so limit depth.
parent: 8 yes, 8 no
left: 6 yes, 2 no
right: 2 yes, 6 no37. How do you choose k for k-means, and when is k-means the wrong tool?
Run k-means for a range of k and plot inertia; the elbow, where the gains flatten, is a candidate. The silhouette score is more reliable: for each point it compares the mean distance to its own cluster, a, with that to the nearest other cluster, b, as (b - a) / max(a, b), so a = 2 and b = 5 give 0.6. Combine this with business sense, such as segments a marketing team can act on. k-means fails on elongated or uneven clusters and is pulled by outliers; DBSCAN or Gaussian mixtures may fit better.
38. What does R-squared tell you, and can it be negative?
R-squared is 1 minus the residual sum of squares over the total sum of squares: the share of the target's variance the model explains. For actuals 120, 150, 90, 200 and predictions 110, 160, 95, 170 it is 1 - 1125 / 6600 = 0.83. On test data it is negative when the model is worse than predicting the mean: predicting 200 for all four gives -2.18. Training R-squared never falls when you add features, so use adjusted R-squared or validation scores: 0.83 with 10 features and 100 rows adjusts to 0.811.
39. Why do deep networks use ReLU instead of sigmoid in hidden layers?
Backpropagation multiplies gradients layer by layer. The sigmoid's derivative is at most 0.25, so across 10 layers the gradient can shrink by a factor of 0.25^10, about 9.5 x 10^-7, and early layers barely learn: the vanishing gradient problem. ReLU has a gradient of 1 for positive inputs, so the signal passes through and training is faster. Its risk is dead units stuck at zero, which Leaky ReLU or careful initialisation and learning rates reduce. Sigmoid is still right at the output for a binary probability.
40. Why use Pipeline and ColumnTransformer instead of preprocessing by hand?
A pipeline chains preprocessing and the model into one estimator, and ColumnTransformer applies different steps to different columns. The benefits: fit learns imputation values, scaling and categories from training data only, so cross-validation and grid search refit them per fold and cannot leak; the exact same transformations run at prediction time, avoiding training-serving skew; and you deploy one saved object. Hand-written preprocessing in a notebook is where most leakage and production mismatches come from.
# imports from sklearn.compose, .pipeline, .impute,
# .preprocessing and .linear_model
num = make_pipeline(SimpleImputer(strategy="median"),
StandardScaler())
prep = ColumnTransformer([
("num", num, ["age", "monthly_spend"]),
("cat", OneHotEncoder(handle_unknown="ignore"), ["city"]),
])
model = make_pipeline(prep, LogisticRegression(max_iter=1000))
model.fit(X_train, y_train)Ready to check yourself? Take the free Machine Learning mock test
Machine Learning interview questions: Hard questions (experienced)
20 questions41. Explain the bias-variance decomposition and how bagging and boosting act on it.
For squared error, the expected test error at a point splits into bias squared, variance and irreducible noise. Bagging averages models trained on resampled data: for B models of variance sigma^2 with pairwise correlation rho, the average has variance rho x sigma^2 + (1 - rho) x sigma^2 / B, so 100 trees with rho = 0.3 cut a variance of 1 to 0.307. More trees never beat the rho term, which is why random forests decorrelate trees with feature sampling. Boosting adds weak, high-bias learners in sequence and mainly cuts bias.
42. Why does L1 regularization give exactly zero coefficients while L2 does not? When would you use Elastic Net?
The L1 penalty has a corner at zero: its slope stays at plus or minus lambda however small the coefficient, so a feature whose pull on the loss is weaker than lambda is set exactly to zero. With orthonormal features, lasso soft-thresholds each least-squares coefficient: with lambda 0.5, 0.3 becomes 0 and 2.0 becomes 1.5, while ridge divides by (1 + lambda), giving 0.2 and 1.333. Among correlated features lasso picks one fairly arbitrarily; Elastic Net mixes L1 and L2 to keep or drop such groups together.
43. Walk through one step of gradient boosting for regression with squared loss.
Start with a constant, the mean, 25. For squared loss the negative gradient is the residual, so fit a small tree to the residuals; a stump that separates the two low values from the two high ones predicts -10 and 10. Add it scaled by the learning rate 0.1: the predictions become 24, 24, 26, 26 and the training MSE falls from 125 to 106. Repeat for hundreds of trees. Other losses, such as log loss or Huber, change only the gradient. A small learning rate with more trees, row subsampling and early stopping generalises best.
y = [10, 20, 30, 40]
F0 = mean(y) = 25
resid = [-15, -5, 5, 15] # negative gradient
stump = [-10, -10, 10, 10] # leaf means of resid
F1 = F0 + 0.1 * stump = [24, 24, 26, 26]44. Why can impurity-based feature importance mislead, and what do you use instead?
The feature_importances_ of tree models measure impurity decrease on the training data. They favour high-cardinality or continuous features, such as a customer ID, that offer many split points, and they can rank highly a feature that only helps the model overfit. Permutation importance on validation data measures how much the score drops when one column is shuffled, so it reflects real generalisation. Both mislead with correlated features: shuffle one and its twin still carries the signal, so group such features or read SHAP with care.
from sklearn.inspection import permutation_importance
r = permutation_importance(model, X_val, y_val, n_repeats=10,
scoring="roc_auc", random_state=0)
for i in r.importances_mean.argsort()[::-1][:5]:
print(X_val.columns[i], round(r.importances_mean[i], 4))45. How would you explain a credit model to business and compliance teams?
Start with what it decides and how well, in business terms: of 100 applicants it flags, how many would default, and how many defaulters it misses, compared with the current rules. For the global picture, show the few factors that matter most and their direction, using permutation importance or partial dependence plots. For one rejected applicant, show local reasons, for example from SHAP values. Say clearly that these are associations, not causes, and check that the model behaves fairly across groups such as gender and region.
46. What is the difference between data drift and concept drift, and how do you monitor them?
Data drift is a change in the input distribution, for example more first-time borrowers after a new marketing channel. Concept drift is a change in the relation between inputs and label, for example fraudsters changing tactics. Monitor inputs and model scores with statistics such as the population stability index: this table gives a PSI of 0.105, which a common rule of thumb calls moderate drift (under 0.1 stable, over 0.25 major). Concept drift shows only in performance once labels arrive, so track delayed metrics too.
score bin expected actual
1 0.25 0.15
2 0.25 0.20
3 0.25 0.30
4 0.25 0.35
PSI = sum((actual - expected) * ln(actual / expected))47. How do you take a scikit-learn model from a notebook to production?
Save the whole pipeline, preprocessing included, not just the model, and record the training data version, code commit, library versions and metrics. Choose batch scoring, such as nightly risk scores, or a real-time API by the latency you need. Pin the same scikit-learn version, because pickled models are not guaranteed to load or behave the same across versions, and never load a pickle from an untrusted source. Validate the input schema in the service, roll out with shadow or canary traffic, and log inputs and predictions.
import joblib
joblib.dump(pipeline, "default_model_v3.joblib")
# in the service, on the same scikit-learn version:
model = joblib.load("default_model_v3.joblib")
proba = model.predict_proba(new_rows)[:, 1]48. What is probability calibration, and when does it matter?
A model is calibrated when, among cases it scores 0.2, about 20% are positive. It matters when the probability itself is used, such as expected loss = probability x loan amount, or when scores from several models are compared. SVMs, random forests, naive Bayes and models trained with class weights or resampling are often miscalibrated. Check with a reliability curve and the Brier score, the mean squared gap between probability and outcome: 0.9, 0.8, 0.3, 0.6 for outcomes 1, 1, 0, 0 give 0.125. Fix with sigmoid or isotonic calibration.
from sklearn.calibration import CalibratedClassifierCV
from sklearn.metrics import brier_score_loss
cal = CalibratedClassifierCV(model, method="isotonic", cv=5)
cal.fit(X_train, y_train)
print(brier_score_loss(y_val, cal.predict_proba(X_val)[:, 1]))49. How would you design validation for a model that predicts loan defaults 12 months ahead?
Split by time, not randomly, to copy how the model will be used: train on older applications and test on later ones. Leave a 12-month gap, because on the day you train, applications from the last year do not have their outcome yet. Build every feature as of the application date, so bureau data or repayment counts from later are excluded. Repeat over several cut-off dates, a rolling backtest, to see how stable the results are, and compare against the current scorecard on the same out-of-time set.
train: applications from months 1-24 (outcomes known)
gap: months 25-36 (outcome not yet known on the
simulated training date)
test: applications from months 37-4250. What is nested cross-validation, and why is best_score_ from a grid search optimistic?
best_score_ is the maximum over many candidates scored on the same folds, so part of it is luck: you picked the setting that happened to suit those folds. Nested cross-validation puts the whole search inside an outer loop: each outer fold runs its own grid search on the rest of the data and scores the winner on the held-out fold. The outer mean is an honest estimate of the whole procedure. It is costly, 185 fits here, so use it to compare modelling approaches or on small data, not on every run.
from sklearn.model_selection import GridSearchCV, cross_val_score
inner = GridSearchCV(pipe, grid, cv=3, scoring="roc_auc") # 12 combos
outer = cross_val_score(inner, X, y, cv=5, scoring="roc_auc")
print(outer.mean()) # 5 x (3 x 12 + 1 refit) = 185 fits51. Grid search is too slow for your gradient boosting model. What do you do?
Grid search spends most fits in poor regions and grows exponentially with the number of parameters. Random search samples settings from distributions, log-uniform for learning rate and regularization, and finds good values in far fewer trials. Successive halving goes further: it tries many candidates on a small budget, such as few samples, and gives more only to the best third each round; in scikit-learn 1.4-1.5 it still needs the experimental import shown. Also use early stopping, so the number of trees (max_iter here) need not be tuned.
from scipy.stats import loguniform, randint
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.experimental import enable_halving_search_cv # noqa
from sklearn.model_selection import HalvingRandomSearchCV
space = {"learning_rate": loguniform(0.01, 0.3),
"max_leaf_nodes": randint(15, 64),
"l2_regularization": loguniform(1e-3, 10)}
search = HalvingRandomSearchCV(HistGradientBoostingClassifier(),
space, factor=3, random_state=0)52. How do gamma and C interact in an RBF SVM, and why do SVMs struggle on large datasets?
The RBF kernel is exp(-gamma x squared distance). For two points at squared distance 2, gamma 0.5 gives a similarity of 0.368 but gamma 5 gives 4.5 x 10^-5, so a large gamma makes each support vector influence only close neighbours and the boundary becomes wiggly, while a small gamma makes it smooth. C sets how hard errors are punished. Large gamma with large C overfits, so tune both together on a log grid. Kernel SVM fit time grows at least quadratically with samples; for big data use LinearSVC or a kernel approximation.
53. What is the curse of dimensionality, and how does it affect KNN?
As dimensions grow, data becomes sparse and distances lose contrast: the nearest and farthest neighbours end up at similar distances, so nearest stops meaning similar. One view: in a unit cube, the inner cube of side 0.9 holds 0.81 of the volume in 2 dimensions, 0.349 in 10 and about 2.7 x 10^-5 in 100, so almost every point lies near the edge. KNN and other distance-based methods degrade, and the data needed to cover the space grows exponentially. Remedies: feature selection, PCA or learned embeddings.
54. Fraud is 0.1% of transactions at a payments company. How do you evaluate and set up the model?
Accuracy and even ROC-AUC look flattering here. A random model has a PR-AUC equal to the prevalence, 0.001, so PR-AUC and precision at a fixed alert volume are more honest. Do the operations maths: out of 1,000,000 transactions with 1,000 frauds, catching 80% while flagging 2% means 20,000 alerts for 800 frauds, a precision of 4%, which may swamp the review team. Use time-based splits, class weights or downsampling of the majority, calibrated scores, and a threshold set by review capacity and fraud loss.
55. How do you encode a high-cardinality feature such as merchant ID or pin code?
One-hot encoding of thousands of pin codes creates a huge sparse matrix. Target encoding replaces each category with the mean of the target for it, shrunk towards the global mean for rare categories. Done naively it leaks, since a row's own label is in its encoding. scikit-learn's TargetEncoder (1.3 and later) cross-fits inside fit_transform, so training rows are encoded from other folds; calling fit and then transform on the training data does not do this. Alternatives: frequency encoding, hashing or native categorical support in boosting.
from sklearn.preprocessing import TargetEncoder
enc = TargetEncoder(smooth="auto", cv=5, random_state=0)
X_tr_enc = enc.fit_transform(X_train[["pin_code"]], y_train)
X_val_enc = enc.transform(X_val[["pin_code"]])56. Your new model beats the old one offline. How do you prove it is better in production?
Offline gains do not always survive production: live data differs, users react, and the metric you optimised may not be the business metric. Run the new model in shadow mode first, scoring live traffic without acting on it, to check latency, errors and score distributions. Then run an A/B test or a gradual canary: send a random share of traffic to the new model and compare a pre-agreed business metric, such as approved loans that later default, with guardrails like latency and complaints. Fix the sample size and duration in advance.
57. When is PCA the wrong choice, and what alternatives would you consider?
PCA keeps directions of high variance, but the signal for the target may sit in a low-variance direction, so applying it blindly before a supervised model can drop exactly what matters; validate with the model score. It is linear, so it misses curved structure, and its components are hard to explain. For sparse text features use TruncatedSVD, which works without centring the data. For visualising clusters, t-SNE or UMAP show local structure but distort global distances and are not meant as model inputs.
58. A model that scored well offline is performing badly a month after launch. How do you debug it?
Work from the data in. First, training-serving skew: log live features and compare them with the training features for the same IDs; a unit change, a new category or a broken join often shows up here. Second, leakage that made offline scores too good: check whether any feature is filled only after the outcome. Third, drift: compare score and feature distributions over time with PSI. Fourth, labels: delayed or redefined labels can distort the metrics. Then choose between a fix, a retrain on recent data or a rollback.
59. Would you use a neural network or gradient boosting for tabular data? How many parameters does this MLP have?
For most tabular business data, such as credit, churn or demand, gradient boosted trees are the strong default: they handle mixed scales, missing values and interactions with little tuning. Neural networks win with images, text, audio or very large data, and when learned embeddings of IDs help. This MLP has 20 x 64 + 64 + 64 x 32 + 32 + 32 x 1 + 1 = 3,457 weights and biases, needs scaled inputs and careful tuning. I would benchmark both under the same cross-validation before deciding.
from sklearn.neural_network import MLPClassifier
mlp = MLPClassifier(hidden_layer_sizes=(64, 32), early_stopping=True)
# 20 inputs -> 64 -> 32 -> 1 output (binary target)60. Why is logistic regression trained with log loss and not mean squared error?
Log loss is the negative log-likelihood of the labels under the predicted probabilities, so minimising it is maximum likelihood. For a positive case, predicting 0.9 costs -ln(0.9) = 0.105, 0.6 costs 0.511, and a confident wrong 0.1 costs 2.303, so confident mistakes are punished hard. With the sigmoid, log loss is convex in the weights, so there is one global minimum, and its gradient is simply (p - y) times the features. MSE with a sigmoid is non-convex, and its gradient vanishes when the model is confidently wrong.
Practice questions for November 2026
New every month3 questions from the Machine Learning mock test, the way they appear in it. A new set comes on the 1st of every month. Pick your answer, then open it.
-
What is the main difference between boosting and bagging?
- Boosting removes the rows that are hard to predict before training
- Boosting trains trees one after another, each fixing earlier errors
- Boosting trains all its trees at once on bootstrap samples and averages them
- Boosting uses one very deep tree instead of many shallow ones
Show the answer
Answer: Boosting trains trees one after another, each fixing earlier errors. Bagging (as in random forests) trains independent trees in parallel and averages them to cut variance. Boosting is sequential: each new tree focuses on what the current ensemble still gets wrong, cutting bias.
-
A classifier outputs the logits [3.0, 1.0, 0.0] for three classes. What softmax probability does it give the first class?
- 0.11
- 0.75
- 0.84
- 0.95
Show the answer
Answer: 0.84. Softmax = e^z_i / sum(e^z): e^3 = 20.09, e^1 = 2.72 and e^0 = 1, so 20.09 / 23.80 = 0.84. Dividing the raw logits gives 0.75, and sigmoid(3) = 0.95 ignores the other classes.
-
scikit-learn's ElasticNet adds alpha * l1_ratio * ||w||_1 + 0.5 * alpha * (1 - l1_ratio) * ||w||^2. For alpha=0.2, l1_ratio=0.5 and w = [2, -1], what is the penalty?
- 0.80
- 0.40
- 1.60
- 0.55
Show the answer
Answer: 0.55. The L1 part is 0.2 x 0.5 x 3 = 0.30 and the L2 part is 0.5 x 0.2 x 0.5 x 5 = 0.25, total 0.55. Forgetting the 0.5 on the squared term gives 0.80, and alpha times both norms gives 1.60.
Ready to apply?
There are 1,217 open jobs asking for Machine Learning on CoreHRx right now. Create a free profile once and apply with it.