☰

Top 77 Machine Learning Interview Questions and Answers

Machine Learning Interview Questions and Answers

Machine learning interviews test more than definitions. Employers want to see that you can frame a problem, prepare data, pick and tune a model, choose the right metric and explain the result to someone outside the team. This guide gives you 77 machine learning interview questions and answers organised in the order those skills usually come up, from fundamentals to scenario questions.

Every answer starts with a short, direct response that you can say aloud in an interview, then adds detail, a comparison table or working code where that helps. The code uses scikit learn and was run before it was added to the page, so the printed output under each sample is the real result. Formulas are written out in words, so nothing depends on an image.

The guide has ten parts: 12 fundamentals questions, 9 on data preparation, 10 on supervised algorithms, 6 on unsupervised learning, 9 on evaluation and metrics, 6 on ensembles, 7 on deep learning, 7 on large language models, 5 on deployment and monitoring, and 6 scenario based questions. Use the contents list below to jump to your level.

Key points

  • 77 questions in ten parts, from fundamentals to scenarios.
  • Each answer opens with a short answer you can say in an interview.
  • Code samples were run with scikit learn and show real output.
  • Includes a metrics table, an algorithm comparison and a guide to prompting, RAG and fine tuning.
  • Scenario answers show what the interviewer is listening for.

In this guide

What this guide covers
Section Questions
Machine Learning Fundamentals Q1 to Q12
Data Preparation and Feature Engineering Q13 to Q21
Supervised Learning Algorithms Q22 to Q31
Unsupervised Learning Q32 to Q37
Model Evaluation and Metrics Q38 to Q46
Ensemble Methods Q47 to Q52
Deep Learning Basics Q53 to Q59
Large Language Models and Generative AI Basics Q60 to Q66
MLOps and Deployment Q67 to Q71
Scenario Based Machine Learning Interview Questions Q72 to Q77

Machine Learning Fundamentals

Every machine learning interview starts here. These questions check whether you understand how a model learns, how it fails and how you know it is working, before any algorithm is discussed.

Q1. What is machine learning, and how is it different from traditional programming?

Machine learning builds a model that learns patterns from data instead of following rules written by a programmer. In traditional programming you supply rules and data and get answers. In machine learning you supply data and known answers, and the algorithm finds the rules, which you then apply to new data.

Machine learning suits problems where the rules are too complex to write by hand, such as spam detection, demand forecasting or image recognition. It does not suit problems that have clear fixed rules, such as calculating tax.

Q2. What are the main types of machine learning?

The main types are supervised learning, which learns from labelled examples, unsupervised learning, which finds structure in unlabelled data, semi supervised learning, which mixes a few labels with many unlabelled records, and reinforcement learning, where an agent learns by receiving rewards for its actions.

Types of machine learning
Type Data needed Typical task Example
Supervised Inputs with known labels Classification, regression Predict whether a customer will leave
Unsupervised Inputs only Clustering, anomaly detection, dimensionality reduction Group customers by behaviour
Semi supervised A few labels and many unlabelled records Classification when labelling is costly Tag documents with a small labelled set
Reinforcement An environment that returns rewards Sequential decisions Game playing, robot control

 

Q3. What is the difference between classification and regression?

Classification predicts a category, such as spam or not spam, and regression predicts a continuous number, such as a house price. The choice decides the algorithms and the metrics you use. Logistic regression, despite its name, is a classification method because it predicts the probability of a class.

Some problems can be framed either way. Predicting a customer age band is classification, while predicting the exact age is regression.

Q4. Why do we split data into training, validation and test sets?

The training set teaches the model, the validation set is used to compare models and tune settings, and the test set is used once at the end to estimate how the final model will perform on unseen data. If you tune on the test set, it stops being an honest measure of real performance.

The stratify argument keeps the share of each class the same in every split, which matters when one class is rare.

python
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

X, y = make_classification(n_samples=1000, n_features=10, weights=[0.8, 0.2], random_state=42)

X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.4, stratify=y, random_state=42)
X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5, stratify=y_temp, random_state=42)

print(len(X_train), len(X_val), len(X_test))
print(round(y_train.mean(), 3), round(y_val.mean(), 3), round(y_test.mean(), 3))
output
600 200 200
0.203 0.205 0.2

 

Q5. What is overfitting, how do you detect it and how do you prevent it?

Overfitting happens when a model learns the training data too closely, including its noise, and performs worse on new data. You detect it when training performance is much better than validation performance. You reduce it with more data, a simpler model, regularisation, early stopping, dropout for neural networks, or cross validation.

Training accuracy climbs to 1.00 as the tree gets deeper, but test accuracy stops improving, so the gap between them keeps widening. That widening gap is the sign of overfitting.

python
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier

X, y = make_classification(n_samples=600, n_features=15, n_informative=4, flip_y=0.1, random_state=1)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.3, random_state=1)

for depth in [2, 5, None]:
    m = DecisionTreeClassifier(max_depth=depth, random_state=1).fit(X_tr, y_tr)
    print(f"max_depth={depth}: train={m.score(X_tr, y_tr):.2f}  test={m.score(X_te, y_te):.2f}")
output
max_depth=2: train=0.74  test=0.72
max_depth=5: train=0.90  test=0.78
max_depth=None: train=1.00  test=0.77

 

Q6. What is underfitting?

Underfitting happens when a model is too simple to capture the pattern in the data, so it performs poorly on both the training set and the test set. You fix it by using a more flexible model, adding useful features, reducing regularisation, or training for longer.

A quick check: if training error is already high, the model is underfitting, and more data alone will not help. If training error is low but validation error is high, the model is overfitting.

Q7. What is the bias variance tradeoff?

Bias is the error from a model being too simple, and variance is the error from a model being too sensitive to the training sample. Total expected error equals bias squared plus variance plus irreducible noise. Making a model more complex lowers bias but raises variance, so you look for the point where the sum is lowest.

Underfitting, good fit and overfitting compared
Problem Bias Variance Training error Validation error Typical fix
Underfitting High Low High High More flexible model, better features
Good fit Moderate Moderate Low Close to training error Keep and monitor
Overfitting Low High Very low Much higher Regularise, simplify, add data

 

Q8. What is cross validation and why is it used?

Cross validation splits the training data into several folds, trains on all but one fold, tests on the held out fold, and repeats until every fold has been the test fold. The average score is a more stable estimate of performance than a single split, especially on small datasets.

python
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold, cross_val_score

X, y = make_classification(n_samples=500, n_features=8, random_state=7)
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=7)

scores = cross_val_score(LogisticRegression(max_iter=1000), X, y, cv=cv)
print(scores.round(3))
print("mean:", scores.mean().round(3), "std:", scores.std().round(3))
output
[0.81 0.84 0.85 0.85 0.89]
mean: 0.848 std: 0.026

 

Q9. What is the difference between a parameter and a hyperparameter?

Parameters are values the model learns from data during training, such as the coefficients in linear regression. Hyperparameters are settings you choose before training, such as tree depth, learning rate or the number of neighbours. You tune hyperparameters with a validation set or cross validation.

Besides grid search, random search and Bayesian optimisation are common. Random search often finds a good setting faster when there are many hyperparameters.

python
from sklearn.datasets import make_classification
from sklearn.model_selection import GridSearchCV
from sklearn.neighbors import KNeighborsClassifier

X, y = make_classification(n_samples=400, n_features=6, random_state=3)

search = GridSearchCV(KNeighborsClassifier(), {"n_neighbors": [1, 3, 5, 9, 15]}, cv=5)
search.fit(X, y)

print(search.best_params_, round(search.best_score_, 3))
output
{'n_neighbors': 9} 0.942

 

Q10. What is data leakage and how can you avoid it?

Data leakage happens when information from outside the training data, often from the test set or the future, influences the model, which makes the evaluation look better than real performance. Common causes are scaling or imputing before splitting, features built from the target, and random splits on time ordered data.

Putting preprocessing inside a pipeline is the simplest protection, because every step is fitted only on the training part of each fold.

python
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression

X, y = make_classification(n_samples=300, n_features=20, random_state=5)

# Safe: the scaler is fitted inside each training fold only
pipe = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
print(cross_val_score(pipe, X, y, cv=5).mean().round(3))
output
0.89

 

Q11. What is the curse of dimensionality?

As the number of features grows, the data becomes sparse, because the space to cover grows much faster than the number of rows. Distances between points become less meaningful, models need more data to generalise, and overfitting becomes more likely. Feature selection and dimensionality reduction are the usual remedies.

Q12. What is a loss function, and how does gradient descent use it?

A loss function measures how wrong the model predictions are, for example mean squared error for regression or log loss for classification. Gradient descent adjusts the parameters in small steps in the direction that reduces the loss, and the learning rate controls the step size.

The printed values land close to the true slope of 3 and intercept of 5 used to create the data.

python
import numpy as np

rng = np.random.default_rng(0)
x = rng.uniform(0, 10, 100)
y = 3 * x + 5 + rng.normal(0, 1, 100)

w, b, lr = 0.0, 0.0, 0.01
for step in range(2000):
    pred = w * x + b
    error = pred - y
    w -= lr * (2 * (error * x).mean())
    b -= lr * (2 * error.mean())

print(round(w, 2), round(b, 2))
output
2.99 4.99

 

Data Preparation and Feature Engineering

Most of the work in a real project is data preparation, so interviewers spend a good share of the time here. Expect questions about missing values, imbalance, scaling and pipelines.

Q13. How do you handle missing values?

First find out why the values are missing. Then choose between dropping rows or columns, filling with the mean, median or most frequent value, filling with a model based estimate, or flagging the missingness as a feature. Always learn the fill values from the training data only, and apply them to the test data.

The extra columns mark which values were missing, which can help when missingness itself carries information, for example a customer who skipped an income field.

python
import numpy as np
from sklearn.impute import SimpleImputer

np.set_printoptions(suppress=True)

X = np.array([[25, 50000], [np.nan, 62000], [31, np.nan], [28, 58000]])
imp = SimpleImputer(strategy="median", add_indicator=True)

print(imp.fit_transform(X))
output
[[   25. 50000.     0.     0.]
 [   28. 62000.     1.     0.]
 [   31. 58000.     0.     1.]
 [   28. 58000.     0.     0.]]

 

Q14. How do you handle an imbalanced dataset?

Start by choosing metrics that suit the problem, such as precision, recall and the precision recall curve, instead of accuracy. Then use class weights, resample the training data with undersampling or oversampling, or generate synthetic samples with SMOTE. Always resample only the training folds, never the validation or test data.

python
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import recall_score
from sklearn.model_selection import train_test_split

X, y = make_classification(n_samples=3000, n_features=10, weights=[0.95, 0.05], class_sep=0.8, random_state=11)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=11)

plain = LogisticRegression(max_iter=1000).fit(X_tr, y_tr)
weighted = LogisticRegression(max_iter=1000, class_weight="balanced").fit(X_tr, y_tr)

print("recall without weights:", round(recall_score(y_te, plain.predict(X_te)), 2))
print("recall with weights:   ", round(recall_score(y_te, weighted.predict(X_te)), 2))
output
recall without weights: 0.07
recall with weights:    0.78

 

Q15. What is the difference between normalisation and standardisation?

Normalisation, or min max scaling, rescales values to a fixed range, usually 0 to 1. Standardisation subtracts the mean and divides by the standard deviation, so values have mean 0 and standard deviation 1. Distance based and gradient based models need scaled inputs, while tree based models do not.

python
import numpy as np
from sklearn.preprocessing import MinMaxScaler, StandardScaler

ages = np.array([[20], [25], [30], [45], [60]])

print(MinMaxScaler().fit_transform(ages).ravel().round(2))
print(StandardScaler().fit_transform(ages).ravel().round(2))
output
[0.   0.12 0.25 0.62 1.  ]
[-1.09 -0.75 -0.41  0.62  1.64]

 

Q16. How do you encode categorical variables?

Use one hot encoding for categories with no order, ordinal encoding when the categories have a real order such as low, medium and high, and target or frequency encoding for features with very many categories. Set handle_unknown so the model does not fail on a category it never saw during training.

The unseen city Chennai becomes a row of zeros instead of raising an error.

python
import pandas as pd
from sklearn.preprocessing import OneHotEncoder

train = pd.DataFrame({"city": ["Pune", "Bangalore", "Pune"]})
enc = OneHotEncoder(handle_unknown="ignore").fit(train)

new = pd.DataFrame({"city": ["Pune", "Chennai"]})
print(enc.get_feature_names_out())
print(enc.transform(new).toarray())
output
['city_Bangalore' 'city_Pune']
[[0. 1.]
 [0. 0.]]

 

Q17. How do you detect and treat outliers?

Detect them with the interquartile range rule, z scores, box plots or a model such as Isolation Forest. Then decide whether each one is an error, which you correct or remove, or a rare but valid value, which you keep. Capping values or using robust models and metrics is often safer than deleting rows.

python
import numpy as np

values = np.array([12, 14, 15, 15, 16, 17, 18, 95])
q1, q3 = np.percentile(values, [25, 75])
iqr = q3 - q1
low, high = q1 - 1.5 * iqr, q3 + 1.5 * iqr

print(low, high, values[(values < low) | (values > high)])
output
11.0 21.0 [95]

 

Q18. What are the main feature selection methods?

Filter methods score each feature on its own, for example with correlation or mutual information. Wrapper methods train models on feature subsets and keep the best subset, as recursive feature elimination does. Embedded methods select features during training, as Lasso regression and tree based importance do.

python
from sklearn.datasets import make_regression
from sklearn.feature_selection import SelectKBest, f_regression

X, y = make_regression(n_samples=200, n_features=8, n_informative=3, random_state=2)
sel = SelectKBest(f_regression, k=3).fit(X, y)

print(sel.get_support(indices=True))
output
[0 1 2]

 

Q19. What is PCA and when would you use it?

Principal component analysis turns correlated features into a smaller set of uncorrelated components that keep as much variance as possible. Use it to reduce dimensions, remove multicollinearity, speed up training or visualise data. Scale the features first, and remember that components are harder to explain than original features.

python
from sklearn.datasets import load_wine
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

X = StandardScaler().fit_transform(load_wine().data)
pca = PCA().fit(X)

print(pca.explained_variance_ratio_[:4].round(3))
print("components for 80 percent variance:", (pca.explained_variance_ratio_.cumsum() < 0.80).sum() + 1)
output
[0.362 0.192 0.111 0.071]
components for 80 percent variance: 5

 

Q20. What is feature engineering? Give examples.

Feature engineering is creating inputs that make the pattern easier for the model to learn, using domain knowledge. Examples include ratios such as spend per visit, date parts such as day of week, rolling averages, counts of past events, text length, and interaction terms between two features.

python
import pandas as pd

df = pd.DataFrame({
    "order_date": pd.to_datetime(["2026-01-03", "2026-01-10", "2026-02-02"]),
    "amount": [1200, 800, 1500],
    "items": [3, 2, 5],
})

df["day_of_week"] = df["order_date"].dt.day_name()
df["amount_per_item"] = df["amount"] / df["items"]
print(df)
output
  order_date  amount  items day_of_week  amount_per_item
0 2026-01-03    1200      3    Saturday            400.0
1 2026-01-10     800      2    Saturday            400.0
2 2026-02-02    1500      5      Monday            300.0

 

Q21. Why should you use a pipeline in scikit learn?

A pipeline chains preprocessing and the model into one object, so the same steps run in the same order during training and prediction. It prevents data leakage, makes cross validation honest, and lets you save and deploy the whole workflow as a single file.

python
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler

df = pd.DataFrame({"age": [25, 40, None, 33, 52, 29],
                   "city": ["Pune", "Bangalore", "Pune", "Pune", "Bangalore", "Bangalore"],
                   "bought": [0, 1, 0, 1, 1, 0]})

prep = ColumnTransformer([
    ("num", Pipeline([("imp", SimpleImputer(strategy="median")), ("sc", StandardScaler())]), ["age"]),
    ("cat", OneHotEncoder(handle_unknown="ignore"), ["city"]),
])

model = Pipeline([("prep", prep), ("clf", LogisticRegression())]).fit(df[["age", "city"]], df["bought"])
print(model.predict(pd.DataFrame({"age": [30], "city": ["Pune"]})))
output
[0]

 

Supervised Learning Algorithms

These questions test whether you know how the common algorithms work, what they assume and when each one is a good choice. Explain the idea in plain words first, and add the maths only if asked.

Q22. What are the assumptions of linear regression?

Linear regression assumes a linear relationship between the features and the target, independent errors, constant error variance, errors that are roughly normally distributed, and no strong multicollinearity among the features. You check them with residual plots, a Q Q plot, the Durbin Watson test and variance inflation factors.

Normal errors matter mainly for confidence intervals and tests. The coefficients can still be estimated without it, so say that if you are asked which assumption is least critical.

Q23. How does logistic regression work, and why does it have regression in its name?

Logistic regression computes a weighted sum of the features and passes it through the sigmoid function, which squeezes the result between 0 and 1 so it can be read as the probability of the positive class. The name comes from the regression step before the sigmoid. The final prediction is a class, chosen with a threshold.

python
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression

X, y = make_classification(n_samples=300, n_features=4, random_state=0)
model = LogisticRegression().fit(X, y)

print(model.predict_proba(X[:3]).round(3))
print(model.predict(X[:3]))
output
[[0.034 0.966]
 [0.999 0.001]
 [0.949 0.051]]
[1 0 0]

 

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

Both add a penalty on large coefficients to reduce overfitting. L1, used in Lasso, adds the sum of absolute coefficients and can shrink some coefficients exactly to zero, so it also selects features. L2, used in Ridge, adds the sum of squared coefficients and shrinks all of them without removing any.

Elastic Net combines both penalties and is useful when features are correlated.

python
from sklearn.datasets import make_regression
from sklearn.linear_model import Lasso, Ridge

X, y = make_regression(n_samples=200, n_features=10, n_informative=3, noise=5, random_state=4)

print("Lasso zeros:", (Lasso(alpha=1.0).fit(X, y).coef_ == 0).sum())
print("Ridge zeros:", (Ridge(alpha=1.0).fit(X, y).coef_ == 0).sum())
output
Lasso zeros: 7
Ridge zeros: 0

 

Q25. How does a decision tree decide where to split?

At each node the tree tries possible splits and picks the one that makes the child nodes purest. For classification, purity is measured with Gini impurity, which is 1 minus the sum of squared class proportions, or with entropy, which is the negative sum of each proportion times its base 2 logarithm. Regression trees use variance reduction.

A pure node, where every record belongs to one class, has a Gini impurity of 0. A node with an even split between two classes has a Gini impurity of 0.5.

python
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier, export_text

iris = load_iris()
tree = DecisionTreeClassifier(max_depth=2, random_state=0).fit(iris.data, iris.target)

print(export_text(tree, feature_names=iris.feature_names))
output
|--- petal width (cm) <= 0.80
|   |--- class: 0
|--- petal width (cm) >  0.80
|   |--- petal width (cm) <= 1.75
|   |   |--- class: 1
|   |--- petal width (cm) >  1.75
|   |   |--- class: 2

 

Q26. How does a support vector machine work, and what is the kernel trick?

A support vector machine finds the boundary that separates the classes with the widest possible margin, and only the points closest to the boundary, called support vectors, decide where it sits. The kernel trick lets it separate data that is not linearly separable by working as if the data were mapped to a higher dimension, without computing that mapping.

python
from sklearn.datasets import make_moons
from sklearn.model_selection import cross_val_score
from sklearn.svm import SVC

X, y = make_moons(n_samples=300, noise=0.2, random_state=0)

for kernel in ["linear", "rbf"]:
    print(kernel, cross_val_score(SVC(kernel=kernel), X, y, cv=5).mean().round(3))
output
linear 0.843
rbf 0.983

 

Q27. How do you choose k in k nearest neighbours, and why does scaling matter?

Choose k with cross validation. A small k follows the noise and overfits, while a large k smooths too much and underfits. Because kNN classifies by distance, a feature with a large range dominates the result, so scale the features first.

python
from sklearn.datasets import load_wine
from sklearn.model_selection import cross_val_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

X, y = load_wine(return_X_y=True)

raw = cross_val_score(KNeighborsClassifier(5), X, y, cv=5).mean()
scaled = cross_val_score(make_pipeline(StandardScaler(), KNeighborsClassifier(5)), X, y, cv=5).mean()

print("without scaling:", round(raw, 3))
print("with scaling:   ", round(scaled, 3))
output
without scaling: 0.691
with scaling:    0.949

 

Q28. Why is Naive Bayes called naive?

It applies Bayes theorem and assumes that all features are independent of each other given the class, which is rarely true. Despite this, it trains very fast, needs little data and works well for text classification such as spam filtering, where the features are word counts.

python
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

texts = ["win a free prize now", "meeting at 10 tomorrow", "free offer click now", "project report attached"]
labels = ["spam", "ham", "spam", "ham"]

vec = CountVectorizer()
model = MultinomialNB().fit(vec.fit_transform(texts), labels)

print(model.predict(vec.transform(["free prize offer", "report for the meeting"])))
output
['spam' 'ham']

 

Q29. What is multicollinearity, and how do you detect and fix it?

Multicollinearity means two or more features are strongly correlated, which makes coefficient estimates unstable and hard to interpret. Detect it with a correlation matrix or the variance inflation factor, where a value above about 5 to 10 is a warning. Fix it by dropping or combining features, using PCA, or using Ridge regression.

The variance inflation factor of a feature is 1 divided by (1 minus R squared), where R squared comes from regressing that feature on all the others.

python
import numpy as np
import pandas as pd

rng = np.random.default_rng(0)
a = rng.normal(size=200)
df = pd.DataFrame({"a": a, "b": a * 0.95 + rng.normal(scale=0.2, size=200), "c": rng.normal(size=200)})

print(df.corr().round(2))
output
      a     b     c
a  1.00  0.98  0.06
b  0.98  1.00  0.07
c  0.06  0.07  1.00

 

Q30. What is the difference between a generative and a discriminative model?

A discriminative model learns the boundary between classes by modelling the probability of the label given the features. A generative model learns how the data of each class is produced, by modelling the features and the label together, so it can also create new samples. Logistic regression is discriminative, and Naive Bayes is generative.

Q31. What is the difference between ARIMA and SARIMA?

ARIMA models a time series using its own past values (autoregressive order p), differencing to remove trend (d), and past forecast errors (moving average order q). SARIMA adds the same three ideas at the seasonal level, with seasonal orders P, D and Q and a season length m, so it can model repeating patterns such as monthly sales peaks.

Check that the series is stationary after differencing, and validate with a time based split, not a random one.

python
from statsmodels.tsa.statespace.sarimax import SARIMAX

# monthly series: ARIMA(1,1,1) with a yearly seasonal pattern (m = 12)
model = SARIMAX(sales, order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
result = model.fit(disp=False)
forecast = result.forecast(steps=6)

 

Unsupervised Learning

Unsupervised questions test how you find structure when there are no labels, and how you judge a result that has no single right answer.

Q32. How does k means clustering work, and how do you choose k?

K means picks k starting centres, assigns every point to its nearest centre, moves each centre to the mean of its points, and repeats until the assignments stop changing. Choose k with the elbow method, which looks for the point where adding clusters stops reducing the error much, or with the silhouette score, and then check that the clusters make business sense.

python
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
from sklearn.metrics import silhouette_score

X, _ = make_blobs(n_samples=400, centers=4, cluster_std=1.0, random_state=8)

for k in range(2, 7):
    labels = KMeans(n_clusters=k, n_init=10, random_state=8).fit_predict(X)
    print(k, round(silhouette_score(X, labels), 3))
output
2 0.634
3 0.652
4 0.786
5 0.665
6 0.552

 

Q33. How do k means, hierarchical clustering and DBSCAN compare?

K means is fast and works for round clusters of similar size but needs k in advance. Hierarchical clustering builds a tree of clusters that you can cut at any level, but it is slow on large data. DBSCAN finds clusters of any shape from dense regions and marks sparse points as noise, with no need to set k.

K means, hierarchical clustering and DBSCAN compared
Method Needs k Cluster shape Handles noise Scales to large data
K means Yes Round, similar size No Yes
Hierarchical No, you cut the tree Flexible Limited No
DBSCAN No, but needs a radius and minimum points Any shape Yes, marks noise Moderately

 

Q34. What is the difference between k nearest neighbours and k means?

K nearest neighbours is a supervised method that predicts the label of a new point from the labels of its k closest training points. K means is an unsupervised method that groups unlabelled points into k clusters. They share the letter k and the use of distance, and nothing else.

Q35. How do PCA and t SNE differ?

PCA is a linear method that keeps the directions of greatest variance and can transform new data. The t SNE method is nonlinear and keeps local neighbourhoods, which makes it good for visualising clusters in two dimensions, but it cannot transform new data, and distances between clusters in its plots should not be over interpreted.

Q36. How do you detect anomalies in data?

Use statistical rules such as z scores or the interquartile range for simple cases, and models such as Isolation Forest, local outlier factor or autoencoders for complex data. Isolation Forest isolates points with random splits, and anomalies need fewer splits to separate. Validate flagged cases with domain experts, because labels are usually missing.

Rows 200 and 201 are the two planted odd points. The extra flagged row is a normal point near the edge, which shows that detectors also raise some false alarms.

python
import numpy as np
from sklearn.ensemble import IsolationForest

rng = np.random.default_rng(1)
normal = rng.normal(0, 1, size=(200, 2))
odd = np.array([[6, 6], [-7, 5]])
X = np.vstack([normal, odd])

flags = IsolationForest(contamination=0.01, random_state=1).fit_predict(X)
print("flagged rows:", np.where(flags == -1)[0])
output
flagged rows: [136 200 201]

 

Q37. How do recommender systems work?

Collaborative filtering recommends items liked by users with similar behaviour, and content based filtering recommends items similar to ones the user already liked. Most production systems combine both, and add ranking models that use context such as time and device. The cold start problem, where a new user or item has no history, needs special handling.

python
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

# rows are users, columns are courses, values are ratings (0 means not rated)
ratings = np.array([[5, 4, 0, 1],
                    [4, 5, 1, 0],
                    [1, 0, 5, 4],
                    [0, 1, 4, 5]])

sim = cosine_similarity(ratings)
print(sim.round(2))
print("most similar to user 0:", sim[0][1:].argmax() + 1)
output
[[1.   0.95 0.21 0.21]
 [0.95 1.   0.21 0.21]
 [0.21 0.21 1.   0.95]
 [0.21 0.21 0.95 1.  ]]
most similar to user 0: 1

 

Model Evaluation and Metrics

Choosing the right metric is where many candidates lose marks. Always tie the metric to the cost of each kind of mistake in the business problem.

Q38. What is a confusion matrix, and how do precision, recall and F1 come from it?

A confusion matrix counts true positives, false positives, true negatives and false negatives. Precision is true positives divided by all predicted positives, recall is true positives divided by all actual positives, and F1 is the harmonic mean of the two. Precision answers how many alerts were right, and recall answers how many real cases were found.

Classification metrics and when to use them
Metric Formula in words Use it when
Accuracy Correct predictions divided by all predictions Classes are balanced and errors cost the same
Precision True positives divided by predicted positives False alarms are costly, for example blocking a genuine payment
Recall True positives divided by actual positives Missing a case is costly, for example fraud or disease screening
F1 score Harmonic mean of precision and recall You need one number that balances both on imbalanced data
ROC AUC Area under the true positive rate versus false positive rate curve You want to compare ranking quality across thresholds
PR AUC Area under the precision versus recall curve The positive class is rare

 

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

y_true = [1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
y_pred = [1, 1, 0, 0, 1, 0, 0, 0, 0, 0]

print(confusion_matrix(y_true, y_pred))
print("precision:", round(precision_score(y_true, y_pred), 2))
print("recall:   ", round(recall_score(y_true, y_pred), 2))
print("f1:       ", round(f1_score(y_true, y_pred), 2))
output
[[5 1]
 [2 2]]
precision: 0.67
recall:    0.5
f1:        0.57

 

Q39. Why can accuracy be misleading?

On imbalanced data a model can score high accuracy by always predicting the majority class. If 98 percent of transactions are genuine, a model that never flags fraud is 98 percent accurate and completely useless. Use precision, recall, F1 and the precision recall curve instead.

python
from sklearn.dummy import DummyClassifier
from sklearn.datasets import make_classification
from sklearn.metrics import accuracy_score, recall_score

X, y = make_classification(n_samples=2000, weights=[0.98, 0.02], random_state=0)
model = DummyClassifier(strategy="most_frequent").fit(X, y)
pred = model.predict(X)

print("accuracy:", round(accuracy_score(y, pred), 3))
print("recall:  ", recall_score(y, pred))
output
accuracy: 0.975
recall:   0.0

 

Q40. What is the difference between ROC and AUC?

The ROC curve plots the true positive rate against the false positive rate as the classification threshold changes. AUC is a single number, the area under that curve. An AUC of 0.5 means the model ranks no better than chance and 1.0 means a perfect ranking.

python
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score, roc_curve
from sklearn.model_selection import train_test_split

X, y = make_classification(n_samples=1000, random_state=1)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, random_state=1)
scores = LogisticRegression(max_iter=1000).fit(X_tr, y_tr).predict_proba(X_te)[:, 1]

fpr, tpr, thresholds = roc_curve(y_te, scores)
print("points on the curve:", len(thresholds))
print("AUC:", round(roc_auc_score(y_te, scores), 3))
output
points on the curve: 58
AUC: 0.923

 

Q41. When would you use a precision recall curve instead of ROC AUC?

Use the precision recall curve when the positive class is rare. ROC AUC can look good because the large number of true negatives keeps the false positive rate low, even when many of the flagged cases are wrong. The precision recall curve focuses on the positive class and shows that weakness directly.

Q42. Which metrics do you use for regression models?

Mean absolute error is the average size of the errors and is easy to explain. Mean squared error penalises large errors more, and root mean squared error returns it to the original unit. R squared shows the share of variance explained. Choose MAE when outliers should not dominate and RMSE when large misses are especially costly.

python
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score

y_true = [100, 150, 200, 250]
y_pred = [110, 140, 210, 300]

print("MAE: ", mean_absolute_error(y_true, y_pred))
print("RMSE:", round(mean_squared_error(y_true, y_pred) ** 0.5, 2))
print("R2:  ", round(r2_score(y_true, y_pred), 3))
output
MAE:  20.0
RMSE: 26.46
R2:   0.776

 

Q43. How do you choose a classification threshold?

The default threshold of 0.5 is rarely the best. Choose the threshold from the cost of each error: lower it to catch more positives at the cost of more false alarms, raise it to be more selective. Plot precision and recall against the threshold, or compute the expected business cost at each threshold, and pick the best point on validation data.

python
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import precision_score, recall_score
from sklearn.model_selection import train_test_split

X, y = make_classification(n_samples=2000, weights=[0.9, 0.1], random_state=2)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=2)
p = LogisticRegression(max_iter=1000).fit(X_tr, y_tr).predict_proba(X_te)[:, 1]

for t in [0.2, 0.35, 0.5, 0.7]:
    pred = (p >= t).astype(int)
    print(t, "precision", round(precision_score(y_te, pred), 2), "recall", round(recall_score(y_te, pred), 2))
output
0.2 precision 0.62 recall 0.98
0.35 precision 0.7 recall 0.94
0.5 precision 0.81 recall 0.82
0.7 precision 0.86 recall 0.71

 

Q44. What is model calibration, and why does it matter?

A model is calibrated when its predicted probabilities match real frequencies, so cases scored 0.8 are positive about 80 percent of the time. It matters when you use the probability itself, for example to price risk or to rank by expected loss. Check it with a calibration curve and fix it with Platt scaling or isotonic regression.

Q45. How do you cross validate time series data?

Never shuffle a time series. Use a forward chaining split, where each fold trains on earlier periods and tests on the period right after, so the model never sees the future. Scikit learn provides TimeSeriesSplit for this. A random split would leak future information into training.

python
import numpy as np
from sklearn.model_selection import TimeSeriesSplit

X = np.arange(12).reshape(-1, 1)

for train, test in TimeSeriesSplit(n_splits=3).split(X):
    print("train", train.tolist(), "test", test.tolist())
output
train [0, 1, 2] test [3, 4, 5]
train [0, 1, 2, 3, 4, 5] test [6, 7, 8]
train [0, 1, 2, 3, 4, 5, 6, 7, 8] test [9, 10, 11]

 

Q46. How do you decide whether one model is really better than another?

Compare them on the same cross validation folds and look at the spread of the scores, not only the average. If the difference is smaller than the fold to fold variation, treat the models as equal and prefer the simpler or cheaper one. For important decisions, confirm with a held out test set or an online A B test.

python
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import RepeatedStratifiedKFold, cross_val_score

X, y = make_classification(n_samples=600, n_features=12, random_state=9)
cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=3, random_state=9)

for name, model in [("logistic", LogisticRegression(max_iter=1000)), ("forest", RandomForestClassifier(random_state=9))]:
    s = cross_val_score(model, X, y, cv=cv)
    print(name, "mean", s.mean().round(3), "std", s.std().round(3))
output
logistic mean 0.816 std 0.022
forest mean 0.873 std 0.023

 

Ensemble Methods

Ensembles power most winning tabular data solutions. Interviewers want to see that you can explain why combining models works and when each method fits.

Q47. What is the difference between bagging and boosting?

Bagging trains many models in parallel on random samples of the data and averages them, which mainly reduces variance. Boosting trains models one after another, where each new model focuses on the mistakes of the previous ones, which mainly reduces bias. Random forest is a bagging method, and gradient boosting is a boosting method.

Bagging compared with boosting
Point Bagging Boosting
Training Models trained in parallel Models trained in sequence
Main effect Reduces variance Reduces bias
Overfitting risk Lower Higher, needs tuning and early stopping
Examples Random forest, bagged trees AdaBoost, gradient boosting, XGBoost, LightGBM

 

Q48. How does a random forest work?

A random forest builds many decision trees, each on a bootstrap sample of the rows and with a random subset of features considered at every split. The trees are deliberately different from each other, and averaging their votes cancels much of their individual noise. The result is more accurate and more stable than one tree.

python
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier

X, y = make_classification(n_samples=800, n_features=15, n_informative=5, flip_y=0.08, random_state=6)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, random_state=6)

tree = DecisionTreeClassifier(random_state=6).fit(X_tr, y_tr)
forest = RandomForestClassifier(n_estimators=200, random_state=6).fit(X_tr, y_tr)

print("single tree:  ", round(tree.score(X_te, y_te), 3))
print("random forest:", round(forest.score(X_te, y_te), 3))
output
single tree:   0.825
random forest: 0.91

 

Q49. What is the out of bag score in a random forest?

Each tree is trained on a bootstrap sample that leaves out roughly one third of the rows. Those left out rows are out of bag for that tree, so they can be used to test it. Averaging these predictions gives a validation estimate without a separate validation set.

python
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier

X, y = make_classification(n_samples=800, n_features=15, random_state=6)
rf = RandomForestClassifier(n_estimators=200, oob_score=True, random_state=6).fit(X, y)

print("OOB accuracy:", round(rf.oob_score_, 3))
output
OOB accuracy: 0.924

 

Q50. How is gradient boosting different from a random forest, and what are XGBoost and LightGBM?

Gradient boosting builds shallow trees one at a time, each fitted to the errors left by the trees before it, using gradient descent on a loss function. XGBoost and LightGBM are fast, regularised implementations of it with support for missing values and large data. A random forest builds deep trees independently and averages them.

Early stopping picks the number of trees using a validation split, which protects boosting from overfitting.

python
from sklearn.datasets import make_classification
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.model_selection import train_test_split

X, y = make_classification(n_samples=2000, n_features=20, n_informative=8, random_state=12)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, random_state=12)

gb = HistGradientBoostingClassifier(learning_rate=0.1, max_iter=200, early_stopping=True, random_state=12).fit(X_tr, y_tr)

print("test accuracy:", round(gb.score(X_te, y_te), 3))
print("trees used:", gb.n_iter_)
output
test accuracy: 0.928
trees used: 65

 

Q51. What is stacking?

Stacking trains several different base models and then trains a final model, called a meta learner, to combine their predictions. The base models should make different kinds of errors, for example a linear model, a forest and a kNN. Use out of fold predictions to train the meta learner, so it does not learn from leaked information.

python
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier, StackingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.neighbors import KNeighborsClassifier

X, y = make_classification(n_samples=600, n_features=12, random_state=10)

stack = StackingClassifier(
    estimators=[("rf", RandomForestClassifier(random_state=10)), ("knn", KNeighborsClassifier())],
    final_estimator=LogisticRegression(max_iter=1000))

print(cross_val_score(stack, X, y, cv=5).mean().round(3))
output
0.937

 

Q52. How do you explain which features drive a model?

Tree based importance is quick but biased toward features with many distinct values. Permutation importance measures how much the score drops when one feature is shuffled, and works for any model. SHAP values give each feature a contribution for every single prediction, which supports both global and individual explanations.

Shuffling the second feature hurts the score the most, while features near zero add almost nothing. Scores slightly below zero are just noise.

python
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.inspection import permutation_importance
from sklearn.model_selection import train_test_split

X, y = make_classification(n_samples=800, n_features=6, n_informative=2, n_redundant=0, shuffle=False, random_state=3)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, random_state=3)
rf = RandomForestClassifier(random_state=3).fit(X_tr, y_tr)

r = permutation_importance(rf, X_te, y_te, n_repeats=10, random_state=3)
print(r.importances_mean.round(3))
output
[ 0.052  0.376 -0.009  0.004  0.004  0.002]

 

Deep Learning Basics

You do not need to be a deep learning specialist for most machine learning roles, but you are expected to explain how a neural network learns and what the common building blocks do.

Q53. How does a neural network work?

A neural network is made of layers of units. Each unit computes a weighted sum of its inputs, adds a bias and passes the result through an activation function. Layers stacked together can learn complex patterns. Training adjusts the weights to reduce the loss, using backpropagation to find how each weight affects the error.

python
import numpy as np

def relu(x):    return np.maximum(0, x)
def sigmoid(x): return 1 / (1 + np.exp(-x))

x = np.array([0.5, -1.2, 3.0])
W1 = np.array([[0.2, -0.5, 0.1], [0.4, 0.3, -0.2]])   # 3 inputs to 2 hidden units
b1 = np.array([0.1, -0.1])
W2 = np.array([[0.7, -0.3]])                           # 2 hidden units to 1 output
b2 = np.array([0.05])

hidden = relu(W1 @ x + b1)
output = sigmoid(W2 @ hidden + b2)

print(hidden.round(3), output.round(3))
output
[1.1 0. ] [0.694]

 

Q54. Which activation functions are common, and when do you use each?

ReLU is the default for hidden layers because it is simple and avoids vanishing gradients in most cases. Sigmoid outputs a value between 0 and 1 and suits a binary output. Softmax turns a vector of scores into probabilities that add up to 1, for multiclass output. Tanh outputs between minus 1 and 1 and is used in some recurrent networks.

python
import numpy as np

scores = np.array([2.0, 1.0, 0.1])
softmax = np.exp(scores) / np.exp(scores).sum()

print(softmax.round(3), softmax.sum())
output
[0.659 0.242 0.099] 1.0

 

Q55. What is backpropagation, and how do SGD and Adam differ?

Backpropagation uses the chain rule to compute the gradient of the loss for every weight, working backward from the output. An optimiser then updates the weights. Stochastic gradient descent uses small batches and one learning rate. Adam also keeps running averages of the gradient, so it adapts the step size for each weight and often converges faster.

Q56. What is the vanishing gradient problem?

In deep networks the gradient is a product of many small numbers as it flows backward, so it can shrink toward zero and the early layers stop learning. It is common with sigmoid and tanh activations. Using ReLU, careful weight initialisation, batch normalisation and residual connections reduces the problem.

Q57. How do dropout, batch normalisation and early stopping help?

Dropout randomly switches off a share of units during training, which stops units from relying on each other and reduces overfitting. Batch normalisation rescales layer inputs, which stabilises and speeds up training. Early stopping halts training when validation loss stops improving, so the model does not start memorising the training data.

Q58. How do CNNs, RNNs and transformers differ?

Convolutional networks use small filters that slide over data, which suits images. Recurrent networks process a sequence step by step and carry a memory, which suited text and time series before transformers. Transformers use attention to relate every position in a sequence to every other, process it in parallel, and are now the standard for language.

CNN, recurrent networks and transformers compared
Architecture Best known for Main strength Main limit
CNN Images and video Learns local patterns with few parameters Less suited to long range order in text
RNN, LSTM, GRU Sequences and time series Keeps a running memory Slow to train, struggles with long sequences
Transformer Language, increasingly images and audio Parallel training, long range context Memory and compute grow quickly with input length

 

Q59. What is transfer learning and when should you use it?

Transfer learning starts from a model already trained on a large dataset and adapts it to your task, either by training only a new final layer or by fine tuning more of the network. Use it when you have limited labelled data, because the pretrained model already knows general features such as edges in images or grammar in text.

Large Language Models and Generative AI Basics

Most machine learning interviews now include a few questions on language models. Interviewers look for a clear mental model and for practical judgement, not for memorised product names.

Q60. What is a transformer, and what is attention?

A transformer is a neural network built around attention. For each token in the input, attention scores how relevant every other token is, then forms a weighted mix of their information. The scores come from the softmax of the query vectors multiplied by the key vectors, divided by the square root of the key dimension, and that result weights the value vectors.

Each row of the weight matrix adds up to 1. It shows how much one token attends to every token in the sequence.

python
import numpy as np

rng = np.random.default_rng(0)
tokens, dim = 4, 8
Q, K, V = (rng.normal(size=(tokens, dim)) for _ in range(3))

scores = Q @ K.T / np.sqrt(dim)
weights = np.exp(scores) / np.exp(scores).sum(axis=1, keepdims=True)
output = weights @ V

print(weights.round(2))
print(weights.sum(axis=1))
print(output.shape)
output
[[0.3  0.47 0.08 0.14]
 [0.33 0.07 0.25 0.35]
 [0.32 0.39 0.23 0.06]
 [0.1  0.03 0.68 0.18]]
[1. 1. 1. 1.]
(4, 8)

 

Q61. What are tokens and the context window?

A token is a piece of text, often part of a word, that a language model reads and produces. The context window is the maximum number of tokens the model can take in at once, including the prompt, any retrieved documents and the reply. Longer inputs cost more and can reduce accuracy on details in the middle.

Q62. What are embeddings, and how do you measure similarity between them?

An embedding is a list of numbers that represents the meaning of a word, sentence or image, so that similar items sit close together. Cosine similarity, which compares the angle between two vectors, is the usual way to measure closeness. Embeddings power semantic search, clustering and retrieval for language model applications.

python
import numpy as np

def cosine(a, b):
    return a @ b / (np.linalg.norm(a) * np.linalg.norm(b))

# toy vectors, real embeddings have hundreds or thousands of dimensions
python_course = np.array([0.9, 0.1, 0.3])
data_science  = np.array([0.8, 0.2, 0.4])
cooking_class = np.array([0.1, 0.9, 0.2])

print(round(cosine(python_course, data_science), 3))
print(round(cosine(python_course, cooking_class), 3))
output
0.984
0.271

 

Q63. What is retrieval augmented generation, and how does it work?

Retrieval augmented generation, or RAG, first searches your own documents for passages that match the question, then gives those passages to the language model along with the question so it answers from them. It lets the model use current or private information without retraining, and it makes answers easier to check against a source.

This toy version uses word weights to retrieve. Production systems usually retrieve with embeddings, split documents into chunks, and often rerank the results.

python
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

docs = [
    "Refunds are processed within 7 working days of the request.",
    "The Python course runs for 12 weeks with weekend batches.",
    "Support is available on weekdays from 10 AM to 6 PM.",
]
question = "How long does a refund take?"

vec = TfidfVectorizer().fit(docs + [question])
sims = cosine_similarity(vec.transform([question]), vec.transform(docs))[0]
best = sims.argmax()

print("retrieved passage:", docs[best])
print("prompt to the model would include this passage and the question")
output
retrieved passage: Refunds are processed within 7 working days of the request.
prompt to the model would include this passage and the question

 

Q64. When should you use prompting, RAG or fine tuning?

Start with prompting, because it is cheapest and fastest to try. Add RAG when the model needs facts it was not trained on, such as your policies or recent data. Use fine tuning when you need a consistent style, format or specialised behaviour that prompts and retrieval cannot deliver, and you have good examples.

Prompting, RAG and fine tuning compared
Approach Best for Cost and effort Main risk
Prompting Quick tasks, formatting, simple behaviour Lowest Inconsistent on edge cases
RAG Answers grounded in your documents and current data Medium: indexing and retrieval pipeline Poor retrieval leads to wrong answers
Fine tuning Consistent style, domain behaviour, structured output Highest: data, training and re evaluation Needs good labelled examples and retraining as things change

 

Q65. What is hallucination, and how can you reduce it?

Hallucination is when a language model produces fluent text that is false or unsupported. Reduce it by grounding answers in retrieved sources, asking the model to cite them, allowing it to say it does not know, lowering randomness for factual tasks, validating structured output, and checking important claims with rules or a human reviewer.

Q66. How do you evaluate a language model application?

Build a fixed set of realistic questions with reference answers or a scoring guide, and run it after every change to the prompt, model or retrieval. Score with exact checks where possible, human review for quality, and a second model as judge only with spot checks by people. Also track cost, speed and failure cases from real use.

MLOps and Deployment

A model that stays in a notebook creates no value. These questions check that you understand how models are shipped, watched and kept healthy.

Q67. What are the common ways to deploy a machine learning model?

Batch deployment runs the model on a schedule and stores predictions, which suits reports and nightly scoring. Real time deployment serves predictions through an API for each request, which suits fraud checks and recommendations. Edge deployment runs the model on a device. Choose by how fast the answer is needed and how often the inputs change.

Q68. What is the difference between data drift and concept drift?

Data drift means the distribution of the input features changes, for example customers become younger. Concept drift means the relationship between the inputs and the target changes, for example the same behaviour no longer predicts churn. Both reduce accuracy over time, so monitor inputs, predictions and, when labels arrive, real performance.

A very small p value from the Kolmogorov Smirnov test says the live data no longer looks like the training data. Many teams also track the population stability index.

python
import numpy as np
from scipy.stats import ks_2samp

rng = np.random.default_rng(0)
training_age = rng.normal(40, 8, 2000)
live_age_same = rng.normal(40, 8, 2000)
live_age_shifted = rng.normal(34, 8, 2000)

print("no drift p-value:   ", round(ks_2samp(training_age, live_age_same).pvalue, 3))
print("shifted p-value:    ", round(ks_2samp(training_age, live_age_shifted).pvalue, 6))
output
no drift p-value:    0.902
shifted p-value:     0.0

 

Q69. What is training serving skew?

Training serving skew means the features computed during training differ from those computed in production, for example because of different code, different time windows or missing values handled differently. The model then sees data it was never trained on. Share feature code between training and serving, or use a feature store, and test with the same sample in both places.

Q70. How do you track experiments and version models and data?

Record for every run the code version, data version, parameters, metrics and the saved model, so any result can be reproduced. Tools such as MLflow track experiments and a model registry, and DVC or similar tools version datasets. Without this, a team cannot explain why a model changed or roll back safely.

Q71. When should a model be retrained, and how do you do it safely?

Retrain on a schedule, when monitoring shows drift or a drop in performance, or when enough new labelled data arrives. Test the new model against the current one on recent data, release it to a small share of traffic first, and keep the old version ready for rollback. Automate the steps so retraining is repeatable.

Scenario Based Machine Learning Interview Questions

Scenario questions test judgement. There is rarely one right answer, so state your assumptions, ask what you need to know and walk through your reasoning in order.

Q72. Your fraud model has 98 percent accuracy. What do you check next?

Check the class balance first. If only 2 percent of cases are fraud, always predicting genuine also scores 98 percent. Then look at the confusion matrix, recall on fraud cases, precision of the alerts, the precision recall curve and the cost of a missed fraud against the cost of a false alarm, and tune the threshold to that cost.

What the interviewer is listening for: You do not accept a headline number and you connect metrics to business cost.

Q73. Training accuracy is very high but test accuracy is low. What steps do you take?

That points to overfitting, but first rule out leakage and a bad split. Then compare learning curves, simplify the model, add regularisation, remove noisy features, get more data, and use cross validation to tune settings. Re check the final model once on a clean test set.

What the interviewer is listening for: You check for leakage before blaming the model.

Q74. A model that worked well three months ago is now performing worse. What could be wrong?

Suspect drift. Compare the live feature distributions with the training data, check for upstream changes such as a new data source or a changed field definition, and look at whether the target behaviour has changed. Confirm with recent labelled data, then retrain on newer data or fix the pipeline, and add monitoring so you see it sooner next time.

What the interviewer is listening for: You separate data problems, pipeline problems and real change in behaviour.

Q75. You are building a customer churn model. How do you choose the metric and the threshold?

Ask what a retention offer costs and what a lost customer is worth. If missing a churner is far more costly than a wasted offer, favour recall and set a lower threshold. Estimate the expected profit at several thresholds on validation data, pick the best, and report precision at the number of customers the team can actually contact.

What the interviewer is listening for: You turn a vague goal into a metric that matches money and capacity.

Q76. You have 500 features and only 300 rows. How do you proceed?

The risk is overfitting. Remove clearly useless and duplicated features, use a regularised model such as Lasso or Ridge, or reduce dimensions with PCA, and rely on repeated cross validation with all preprocessing inside the folds. Be cautious about complex models, and say that more data would help more than any technique.

What the interviewer is listening for: You recognise the data size limit and pick simple, regularised approaches.

Q77. How would you explain a loan approval model to a non technical stakeholder, and how do you check it for fairness?

Describe the main factors in plain words using feature importance or SHAP, and show a few example cases. For fairness, compare error rates and approval rates across groups, check that features are not acting as proxies for protected traits, document the limits of the model, and keep a human review path for borderline decisions.

What the interviewer is listening for: You can communicate without jargon and you treat fairness as something to measure.

Common questions about machine learning interviews

What topics should I prepare for a machine learning interview?

Prepare the fundamentals such as overfitting, the bias variance tradeoff and cross validation, then data preparation, the common supervised and unsupervised algorithms, evaluation metrics and ensembles. Add deep learning, language model basics and deployment for experienced roles. Be ready to work through a scenario question using your own reasoning.

How much mathematics do I need for a machine learning interview?

You need working knowledge of probability, basic statistics, linear algebra and calculus ideas such as gradients. Most interviews ask you to explain concepts in words and apply them, not to derive proofs. Know what each formula means, for example why the loss falls when you step against the gradient.

Do machine learning interviews include coding?

Yes, most include Python coding. Expect a data manipulation task in pandas, a short modelling task in scikit learn, and sometimes a general coding problem. Practise building a full pipeline from raw data to a validated model, and explain your choices as you write the code.

Should I learn deep learning before applying for a machine learning role?

For most data science and analytics roles, strong skills in classical machine learning are enough, and deep learning basics help. For roles in computer vision, language or research, deeper knowledge is expected. Read the job description, and be able to explain how a neural network learns and when you would use one.

How are machine learning interviews for experienced candidates different?

Interviews for experienced candidates focus less on definitions and more on judgement. You are asked about problem framing, metric choice, data issues, deployment, monitoring and failures you have handled. Prepare two or three real projects and be ready to explain what went wrong and what you changed.

A four week plan to prepare

Use this plan if you have about a month before your interview. Adjust the pace to what you already know.

Four week study plan
Week Focus What to do
Week 1 Fundamentals and data preparation Run every code sample in the first two sections. Rewrite one pipeline from memory.
Week 2 Supervised and unsupervised algorithms For each algorithm, write a two line explanation and one case where you would not use it.
Week 3 Evaluation, ensembles and scenario questions Practise choosing a metric for five business cases, and justify each choice aloud.
Week 4 Deep learning, language models, deployment and mock interviews Build a small retrieval demo, review the deployment section and take a timed mock interview.

 

Practise with a trainer

If you want to practise these questions with a trainer and build projects you can talk about in interviews, you can book a free demo class for the Prwatech Data Science course. It covers Python, statistics and machine learning with hands on work.

Contact the learning team for current batch dates and fees.

Written by Mithun Ray, data engineering trainer at Prwatech. Reviewed by Avishekh Verma. Last updated October,2026.

Popular Tags:

Machine Learning Interview Questions and Answers