Machine learning interview questions
What machine learning interviews check: regression, tree ensembles and the fundamentals underneath them, feature engineering, classification metrics, probability and statistics, deep learning basics, and getting a model into production and keeping it healthy.
39 questions across 17 topics. Answer each out loud, then open the points a strong answer covers.
Embeddings basics: word2vec, contrastive learning, what embeddings capture
Asked in most interview loops
How does word2vec with negative sampling learn word embeddings?
- Skipgram pairs each centre word with words in its window, training the model to predict which words appear nearby.
- Negative sampling swaps the full-vocabulary softmax for binary classification: real neighbour pairs versus a few randomly sampled words.
- It learns a word matrix and a context matrix and usually keeps the word one; window size shifts what similarity the vectors capture.
How are modern sentence embedders trained, and why do negatives and batch size matter?
- Contrastive objectives like InfoNCE pull a query and its matching passage together and push other passages in the batch apart.
- Larger batches give more in-batch negatives, and hard negatives that look similar but are wrong teach finer distinctions than random ones.
- Unlike static word vectors, which give an ambiguous word like “bank” one vector, these models encode meaning from the whole context.
How would you choose an embedding model for a RAG system in your domain, and when would you fine-tune one?
- Shortlist candidates from a public leaderboard, then compare them with recall@k on your own query–passage pairs, since leaderboard rankings may not transfer to your domain.
- Weigh the practical limits too: maximum input length, vector size for storage and latency, language coverage, cost, and any query or document prefixes the model expects.
- Fine-tune with a contrastive loss on in-domain pairs plus hard negatives mined from your current retriever, then re-embed the whole corpus, because old and new vectors aren’t comparable.
Feature engineering and data preparation
Asked in most interview loops
How would you encode a categorical feature with thousands of distinct values?
- One-hot encoding creates a sparse column per category, which suits linear models with a modest vocabulary but grows huge here and leaves rare categories with too few examples to learn from.
- Group rare categories into an “other” bucket, hash values into a fixed number of buckets and accept some collisions, or learn an embedding when a neural network consumes the feature.
- Target encoding replaces each category with a smoothed mean of the label, but it must be fitted out of fold or on separate data, or it leaks the label and overfits.
Only 1% of your training examples are positive; what would you change in how you train the model?
- First check whether the minority class has enough examples to learn from at all; if it does, a standard model with a tuned decision threshold is often enough.
- Downsample the majority class so each batch contains positives, then upweight the kept majority examples by the same factor so predicted probabilities still reflect the real base rate.
- Resample only the training set, keep the true class ratio in validation and test, and compare approaches with precision–recall metrics at the threshold the product will actually use.
Deploying and serving ML models
Asked in most interview loops
When would you serve predictions in batch rather than online?
- Batch inference precomputes predictions on a schedule and stores them for lookup, which suits a bounded set of inputs such as known users or items and tolerates somewhat stale results.
- Online inference scores each request as it arrives, so it handles new or free-form inputs and fresh context, but it must fit a latency budget and needs heavier serving and monitoring.
- A common hybrid precomputes heavy features or candidates in batch and runs a lighter model online, keeping most of the cost offline while still reacting to the current request.
How would you roll out a new model version safely?
- Before any traffic, compare the candidate offline against the current model and a fixed quality bar, and check that it loads and meets latency targets on the real serving stack.
- Run it in shadow mode first, scoring live requests without affecting users, so you can compare its predictions, latency and error rate with production at no risk to users.
- Then send a small canary share of traffic, watch business and system metrics against the old version, and keep the previous model deployed so rollback is a quick configuration switch.
Gradient descent and backpropagation
Asked often
What causes vanishing gradients in deep networks, and how do modern architectures avoid them?
- Backpropagation multiplies local derivatives layer by layer, so when those factors are mostly below one in magnitude the gradient reaching early layers shrinks exponentially with depth.
- Saturating activations make it worse: the sigmoid’s derivative is at most 0.25 and near zero at both tails, so stacked sigmoids leave early layers barely learning.
- ReLU-family activations, careful initialisation such as Xavier or He, normalisation layers and residual connections all help, and residuals give gradients a direct path around each block.
Why do we train with mini-batches instead of full-batch gradient descent, and how does batch size interact with the learning rate?
- Full-batch descent computes the exact gradient over all the data, so every step is expensive; a randomly sampled mini-batch gives an unbiased estimate of it far more cheaply.
- Many cheap, noisy steps usually make more progress per unit of compute than a few exact ones, and the noise can help the optimiser move past saddle points.
- Larger batches use the GPU better and lower gradient noise but give diminishing returns per example, and they usually need a larger learning rate with warmup to train as well.
How does an autograd engine like PyTorch’s actually compute gradients?
- During the forward pass it records each operation and its inputs as a directed acyclic graph, saving whatever values each operation’s local derivative will need later.
- Calling backward walks the graph in reverse topological order, multiplying each node’s upstream gradient by its local derivative and passing the result on to that node’s inputs.
- Gradients from every use of a tensor are summed, which is why .grad accumulates and you zero it each step; reverse mode gets all parameter gradients for a small multiple of the forward cost.
Explain the bias–variance trade-off. How would you tell which one is hurting your model?
- Bias is how far the average model, trained on many different samples, sits from the truth; variance is how much the fitted model changes from one training sample to another.
- Expected squared error splits into bias squared plus variance plus irreducible noise, and adding capacity usually lowers bias while raising variance, giving the classic U-shaped test-error curve.
- High training and validation error close together points to bias; low training error with a large validation gap points to variance, and a learning curve shows whether more data closes it.
What’s the difference between L1 and L2 regularisation, and when would you use each?
- L2 adds λ times the sum of squared weights to the loss, shrinking every weight smoothly towards zero but rarely setting any of them exactly to zero.
- L1 adds λ times the sum of absolute weights; its constant-size pull drives small weights to exactly zero, so the model becomes sparse and effectively selects features.
- Use L1 when you expect few features to matter, L2 when many correlated features each carry signal, since L1 tends to keep one of a correlated group arbitrarily, or elastic net for both.
Your model does well on validation but badly in production. Why might that be?
- Suspect data leakage first: a feature that encodes the label or future information, or preprocessing such as scaling or imputation fitted on the full dataset before splitting.
- The split may not match deployment: random splits on time-ordered or grouped data let the same user or near-duplicates sit on both sides, so split by time or group instead.
- Otherwise the input distribution has shifted, or features are computed differently online than offline, so compare live and training feature distributions to catch drift and training–serving skew.
What to study for Bias/variance, overfitting and regularisation
Classification metrics
Asked often
You’re building a fraud detector where 1 in 200 transactions is fraud. Why is accuracy the wrong metric, and what would you use?
- A model that predicts “not fraud” every time scores 99.5% accuracy while catching nothing, so accuracy says almost nothing when one class is this rare.
- Report precision, the share of flagged transactions that are really fraud, and recall, the share of fraud that gets flagged, plus the area under the precision–recall curve.
- In training, class weights or resampling stop the model ignoring the rare class, but resampling shifts predicted probabilities, so tune the threshold on data with the real class ratio.
Explain the precision–recall trade-off. How would you choose a decision threshold?
- Precision is TP / (TP + FP) and recall is TP / (TP + FN); raising the threshold flags fewer items, so recall falls while precision usually rises.
- Let the costs decide: a cancer screen favours recall because a missed case is serious, while a spam filter favours precision because hiding a real email annoys users.
- Sweep thresholds on held-out data and pick the one that meets a target, such as recall above a set level, or minimises expected cost; F1 or F-beta summarise one point.
When would you prefer a precision–recall curve over a ROC curve?
- A ROC curve plots true positive rate against false positive rate, and its AUC is the probability that a random positive is scored above a random negative.
- When positives are rare, the false positive rate divides by a huge number of negatives, so many false alarms barely move it and ROC AUC can look excellent.
- A precision–recall curve ignores true negatives and shows the false alarm cost directly; a random classifier’s PR AUC equals the positive rate rather than 0.5, so the baseline is clear.
Probability and statistics for ML interviews
Asked often
A test is 99% sensitive and 99% specific, and 1% of people have the disease. If someone tests positive, how likely are they to have it?
- Bayes’ theorem gives P(ill | positive) = P(positive | ill) × P(ill) / P(positive), where P(positive) sums over both the ill and the healthy.
- Here P(positive) = 0.99 × 0.01 + 0.01 × 0.99 = 0.0198, so the posterior is 0.0099 / 0.0198, which is exactly 50%.
- The prior dominates: at low prevalence, false positives from the large healthy group match the true positives, and a second independent positive test lifts the probability to 99%.
What does a p-value actually mean, and how is it commonly misread?
- A p-value is the probability, assuming the null hypothesis is true, of seeing a result at least as extreme as the one observed.
- It is not the probability that the null is true or that the result is due to chance, and a small p-value says nothing about how large or useful the effect is.
- Running many tests or repeatedly peeking at an A/B test inflates false positives, so fix the sample size in advance or correct for multiple comparisons, for example with Bonferroni.
Why is cross-entropy the standard loss for classification? Can you derive it from maximum likelihood?
- Treat the softmax output as a categorical distribution over classes; the likelihood of the training set is the product of the probabilities the model gives each true label.
- Taking the negative log turns that product into a sum, so maximising likelihood is the same as minimising −Σ log p(y_i | x_i), the cross-entropy against one-hot labels.
- With softmax, its gradient with respect to the logits is simply p − y, and unlike squared error on probabilities it keeps a strong gradient on confident wrong answers.
What to study for Probability and statistics for ML interviews
Linear and logistic regression
Asked often
What assumptions does ordinary least squares make, and what goes wrong when each one fails?
- OLS assumes the mean of the target is linear in the features and the errors are independent with constant variance; normal errors are needed only for exact intervals and p-values.
- A non-linear relationship leaves patterns in the residuals and biased predictions, while correlated or unequal-variance errors keep the coefficients unbiased but make their standard errors and p-values wrong.
- Strongly correlated features inflate coefficient variance, so individual weights swing or flip sign between samples even when predictions stay accurate; residual plots and variance inflation factors reveal both problems.
How do you interpret a logistic regression coefficient, and why train with log loss rather than squared error?
- A coefficient is the change in the log-odds of the positive class for a one-unit increase in that feature with the others held fixed, so its exponential is an odds ratio.
- Log loss is the negative Bernoulli log-likelihood and is convex in the weights, whereas squared error on a sigmoid output is non-convex and gives tiny gradients to confidently wrong predictions.
- On linearly separable data the unregularised weights grow without bound as the loss approaches zero, so add L2 regularisation or early stopping, and scale features before comparing coefficients.
Decision trees and tree ensembles
Asked often
How do random forests and gradient boosting differ, and when would you choose each?
- A random forest trains deep trees independently on bootstrap samples, testing a random subset of features at each split, and averaging their predictions mainly reduces variance.
- Gradient boosting adds shallow trees one after another, each fitted to the negative gradient of the loss for the current ensemble and scaled by a learning rate, which mainly reduces bias.
- Forests rarely overfit as trees are added and need little tuning, while boosting usually scores higher on tabular data but needs early stopping and tuning of learning rate, depth and tree count.
Your tree model ranks a customer ID column as its most important feature; what is going on, and how would you check?
- Impurity-based importance is computed on the training data and favours high-cardinality features, because a column with many unique values offers many splits that can fit noise.
- Compute permutation importance on a held-out set instead, which measures how much the validation score drops when that one feature’s values are shuffled.
- Correlated features share or swap credit under both methods, so group or drop them before concluding a feature matters, and treat importance as association with the prediction, not causation.
Walk me through k-means. How do you choose k, and when does it fail?
- K-means alternates assigning each point to its nearest centroid and moving each centroid to the mean of its points, lowering within-cluster squared distance until it converges to a local optimum.
- Because the result depends on the starting centroids, run several initialisations or k-means++ and keep the best run; choose k from the elbow of the loss curve, silhouette scores or downstream usefulness.
- It assumes compact clusters of similar size and is pulled by outliers and unscaled features, so standardise first and consider DBSCAN, Gaussian mixtures or hierarchical clustering for other shapes.
What does PCA compute, and what should you watch out for when you use it?
- PCA finds orthogonal directions of maximum variance in centred data, the eigenvectors of the covariance matrix or right singular vectors of the data matrix, ordered by the variance each explains.
- Scale features first when their units differ, otherwise the leading components simply follow whichever feature has the largest numeric range rather than any shared structure in the data.
- Choose the number of components from the cumulative proportion of variance explained or a scree plot, and remember that high-variance directions are not necessarily the ones that predict the label.
Deep learning basics
Asked often
How do SGD with momentum and Adam update weights, and why might you still choose SGD?
- SGD with momentum keeps a running average of past gradients and steps along it, which damps oscillation across steep directions and speeds progress along shallow ones.
- Adam also tracks a running average of squared gradients and divides each parameter’s step by its square root, giving every parameter its own effective learning rate, with bias correction early on.
- Adam usually converges faster with less tuning, but well-tuned SGD with momentum and a schedule can generalise as well or better on some vision tasks, and it stores less optimiser state.
How do batch normalisation and dropout behave differently in training and inference, and what goes wrong if you forget to switch modes?
- In training, batch norm normalises each feature with the current mini-batch’s mean and variance while updating running averages; at inference it uses those stored running averages instead.
- In training, inverted dropout zeroes each unit with probability p and scales the survivors by 1/(1 − p), so at inference it does nothing and expected activations still match.
- Serving in training mode makes predictions random and dependent on whatever else is in the batch, so call model.eval() before evaluation or serving and model.train() before resuming training.
Monitoring ML models and data drift
Asked often
What is the difference between covariate shift, label shift and concept drift?
- Covariate shift means the input distribution P(X) changes while P(Y|X) stays the same, for example when a marketing campaign brings in users from a new age group.
- Label shift means the output distribution P(Y) changes while P(X|Y) stays the same, for example when a disease becomes more common but its symptoms look exactly as before.
- Concept drift means P(Y|X) itself changes, so the same input now deserves a different answer, as when buyer behaviour changes after a price rise; only labels or good proxies reveal it reliably.
Labels for your model arrive weeks late. How would you monitor it in the meantime?
- Compare the distributions of key input features and of the predictions against a reference window from training or a recent healthy period, using a test such as Kolmogorov–Smirnov or a distance such as PSI.
- Add data quality checks for schema, missing values and out-of-range values, because many apparent quality drops come from broken upstream pipelines rather than a changing world.
- Treat a drift alert as a prompt to investigate rather than an automatic retraining trigger, and confirm real impact with early proxy outcomes, such as clicks or complaints, before acting.
What is point-in-time correctness in a feature store, and what goes wrong without it?
- Each training example must use feature values as they were known at that example’s timestamp, so labels are joined to features with an as-of join on time, not to the latest values.
- Joining on the latest values leaks future information into training, so offline metrics look strong while the model disappoints in production, where only past data is available.
- A feature store keeps timestamped history in an offline store for training and the latest values in a low-latency online store, both computed from one definition to avoid skew.
How would you make a training run reproducible six months from now?
- Version the code, the configuration and the exact data snapshot or query with its time range, since rerunning a query against a changed table silently trains on different data.
- Pin the environment with locked dependency versions or a container image and fix random seeds, accepting that GPU nondeterminism can still cause small differences between runs.
- Log each run’s parameters, data version, metrics and model artefact in an experiment tracker or metadata store, so every served model can be traced back to what produced it.
What to study for ML pipelines, feature stores and reproducibility
A neural network from scratch in NumPy
Specialist topic or differentiator
Write the forward and backward pass for a two-layer MLP with ReLU and a softmax output in NumPy. What does each gradient look like?
- Forward: A = XW₁ + b₁, H = ReLU(A), Z = HW₂ + b₂, then softmax and mean cross-entropy, caching X, A and H for the backward pass.
- Backward starts from dZ = (P − Y) / N for one-hot labels Y, giving dW₂ = HᵀdZ, db₂ as dZ summed over the batch, and dH = dZW₂ᵀ.
- ReLU passes gradient only where A > 0, so dA = dH ⊙ (A > 0) and dW₁ = XᵀdA; check every gradient’s shape matches its parameter and verify with finite differences.
Why can’t you initialise all the weights to zero, and how should you initialise them instead?
- If every weight in a layer starts at the same value, every hidden unit computes the same output and gets the same gradient, so the units never differentiate.
- Random initialisation breaks that symmetry, but its scale matters: too large and activations and gradients explode through the layers, too small and they shrink towards zero.
- Xavier initialisation sets weight variance to 2 / (fan_in + fan_out) for tanh-like activations, and He initialisation uses 2 / fan_in for ReLU; biases can safely start at zero.
Distributed training: DDP, FSDP/ZeRO, tensor and pipeline parallelism
Specialist topic or differentiator
A 7B-parameter model won’t fit on one GPU for training. Where does the memory go, and what would you do?
- Mixed-precision training with Adam needs about 16 bytes per parameter: 2 for 16-bit weights, 2 for gradients and 12 for fp32 master weights, momentum and variance, so roughly 112 GB.
- Activations come on top and grow with batch size, sequence length and depth; activation checkpointing trades recompute for memory, and gradient accumulation keeps the effective batch with smaller micro-batches.
- Shard the weights, gradients and optimiser states across data-parallel GPUs with ZeRO or FSDP, and add tensor or pipeline parallelism when even sharded layers or activations still don’t fit.
How do data, tensor and pipeline parallelism differ, and where would you place each on a cluster?
- DDP replicates the model, feeds each GPU different data and all-reduces gradients during the backward pass; FSDP shards the model instead, all-gathering weights per layer and reduce-scattering gradients.
- Tensor parallelism splits each layer’s matrix multiplies across GPUs and needs collectives inside every layer, so it stays within a node, where links such as NVLink are fastest.
- Pipeline parallelism gives each GPU a group of layers and passes only activations between stages, so it tolerates slower links across nodes, but GPUs idle in a bubble that micro-batches shrink.
Diffusion models basics
Specialist topic or differentiator
How is a diffusion model trained, and how does it generate an image?
- The forward process adds Gaussian noise over many steps until the data becomes pure noise, and x_t can be sampled directly as √ᾱ_t · x₀ + √(1 − ᾱ_t) · ε.
- Training picks a random image, timestep and noise sample, then teaches the network to predict that noise from x_t and t with a simple mean-squared-error loss.
- Generation starts from pure noise and applies the network step by step, each time removing some predicted noise; the many sequential calls make sampling slow, which DDIM-style samplers and distillation reduce.
How does Stable Diffusion turn a text prompt into an image, and what does classifier-free guidance trade off?
- It runs diffusion in the compressed latent space of a VAE, which is far cheaper than working on pixels, and the VAE decoder turns the final latent into an image.
- A text encoder, CLIP’s in the original model, turns the prompt into token embeddings that the U-Net reads through cross-attention layers at every denoising step.
- Classifier-free guidance predicts noise with and without the prompt and moves from the unconditioned prediction towards, and beyond, the conditioned one; a higher scale follows the prompt more closely but reduces diversity.
Time series forecasting
Specialist topic or differentiator
Why can’t you evaluate a forecasting model with a shuffled K-fold split, and what would you do instead?
- Shuffling puts future observations in the training folds, so the model effectively learns from data it would never have when forecasting, and the reported error looks better than reality.
- Backtest with a rolling or expanding forecasting origin: train only on data before each cut-off, forecast the next horizon, and average the error across many cut-offs.
- Leave a gap between training and test windows when lag or rolling features could reach across the boundary, and make each test window as long as the real forecast horizon.
When would you choose ARIMA over gradient boosting on lag features, and when the reverse?
- ARIMA suits a single series or a few, with modest history and mostly linear autocorrelation, and it gives prediction intervals and coefficients you can read, after differencing to stationarity.
- Gradient boosting on lag, rolling and calendar features suits many related series in one global model, with exogenous inputs such as prices or holidays and nonlinear effects.
- Tree models cannot extrapolate beyond the target range seen in training, so detrend or difference first, and benchmark both against a seasonal naïve forecast using a scaled error like MASE.
Questions and answer points are written for SAADHO from each topic’s sources: question types interviewers ask, never leaked questions. How SAADHO is made