interviewDeck

Your one-stop platform to prepare, practice and ace your interviews.

Loading your questions…

All Questions

Filters & tools

ML Fundamentals Interview Questions and Answers

24 hand-picked ML Fundamentals interview questions with detailed answers. Open the interactive version above to search, filter by difficulty, run code, bookmark questions and track your progress.

Supervised vs unsupervised vs reinforcement learning.

  • Supervised — learn from labelled data (input → known output). Tasks: classification, regression. Example: spam detection, house price prediction.
  • Unsupervised — find patterns in unlabelled data. Tasks: clustering, dimensionality reduction, anomaly detection. Example: customer segmentation.
  • Reinforcement — agent learns by interacting with an environment, receiving rewards/penalties. Example: game playing, robotics, RLHF for LLM alignment.

Training vs inference in machine learning.

Training — the model learns patterns from data by adjusting weights to minimise a loss function. Computationally expensive; done offline or periodically.

Inference — using the trained model to make predictions on new, unseen data. Must be fast and reliable for production serving.

Training uses backward propagation and optimisers (Adam, SGD). Inference is forward-pass only.

Classification vs regression.

Classification — predict a category (discrete label). Binary (spam/not spam) or multi-class (cat/dog/bird). Output: class probabilities.

Regression — predict a continuous number. Example: house price, temperature, revenue forecast. Output: a real-valued number.

Same algorithms can do both with different loss functions and output layers.

Overfitting vs underfitting.

Overfitting — model memorises training data (including noise) and fails on new data. Signs: high training accuracy, low validation accuracy. Fixes: more data, regularisation, simpler model, dropout, early stopping.

Underfitting — model is too simple to capture patterns. Signs: poor performance on both training and validation. Fixes: more features, complex model, train longer.

Goal: generalisation — good performance on unseen data.

Explain the bias-variance tradeoff.

Total prediction error = bias² + variance + irreducible noise.

  • High bias (underfitting) — model is too simple, makes systematic errors. Low training AND validation error gap, but both are high.
  • High variance (overfitting) — model is too sensitive to training data fluctuations. Low training error, high validation error.

Increasing model complexity reduces bias but increases variance — find the sweet spot.

Why split data into train, validation, and test sets?

  • Training set (~70-80%) — model learns from this.
  • Validation set (~10-15%) — tune hyperparameters, select model architecture, decide when to stop training.
  • Test set (~10-15%) — final unbiased evaluation, touched once at the end.

Using test data during development causes data leakage — you overfit to the test set indirectly.

from sklearn.model_selection import train_test_split

X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.3)
X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5)

What is k-fold cross-validation?

Split data into k folds. Train on k-1 folds, validate on the remaining fold. Repeat k times, each fold serving as validation once. Average the k scores for a robust performance estimate.

Useful when data is limited — every sample gets to be in both training and validation. Common: k=5 or k=10.

Stratified k-fold preserves class distribution in each fold (important for imbalanced data).

from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X, y, cv=5, scoring='f1')
print(scores.mean(), scores.std())

Common loss functions and when to use them.

  • MSE / MAE — regression. MSE penalises large errors more; MAE is robust to outliers.
  • Cross-entropy / log loss — classification. Measures distance between predicted probabilities and true labels.
  • Hinge loss — SVM classification.
  • Focal loss — classification with class imbalance; down-weights easy examples.

The loss function defines what 'wrong' means — choose one aligned with your business metric.

What is gradient descent?

An optimisation algorithm that iteratively adjusts model weights in the direction that reduces the loss (opposite to the gradient).

  • Batch GD — uses entire dataset per step; accurate but slow.
  • Stochastic GD (SGD) — one sample per step; noisy but fast.
  • Mini-batch GD — compromise; batch of 32–256 samples. Standard in deep learning.
  • Adam — adaptive learning rate; default optimiser for most neural nets.

How does a neural network work at a high level?

A network of layers of neurons connected by weighted edges:

  1. Input layer — receives features.
  2. Hidden layers — transform inputs through weighted sums + activation functions, learning hierarchical features.
  3. Output layer — produces predictions (softmax for classification, linear for regression).

Training: forward pass (compute output) → compute loss → backward pass (backpropagation computes gradients) → update weights.

Common activation functions and their roles.

  • ReLUmax(0, x). Default for hidden layers. Fast, avoids vanishing gradient. Dead ReLU problem (neurons output 0 forever).
  • Sigmoid — squashes to (0,1). Used for binary classification output. Vanishing gradient in deep networks.
  • Softmax — multi-class output; probabilities sum to 1.
  • Tanh — squashes to (-1,1). Sometimes used in hidden layers.
  • GELU / Swish — smooth variants used in transformers (BERT, GPT).

CNN vs RNN vs Transformer — when to use each?

ArchitectureStrengthTypical use
CNNSpatial/local patterns via convolutionsImage classification, object detection, medical imaging
RNN/LSTMSequential data, temporal dependenciesTime series, early NLP (largely replaced)
TransformerLong-range dependencies, parallel trainingNLP, vision (ViT), multimodal, LLMs

Transformers dominate NLP and are increasingly used for vision, audio, and tabular data too.

What is feature engineering and why does it matter?

Transforming raw data into informative inputs the model can learn from. Examples:

  • Normalising/scaling numerical features (StandardScaler, MinMaxScaler)
  • Encoding categoricals (one-hot, label encoding, target encoding)
  • Creating derived features (age from birthdate, day-of-week from timestamp)
  • Handling missing values (imputation, indicator columns)
  • Text: TF-IDF, tokenisation (or use embeddings for deep learning)

Deep learning reduces manual feature engineering but preprocessing still matters.

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)  # use train stats!

Precision, recall, and F1 score.

  • Precision — of predicted positives, how many are actually positive? TP / (TP + FP). "When I say yes, how often am I right?"
  • Recall — of actual positives, how many did I catch? TP / (TP + FN). "Of all real positives, how many did I find?"
  • F1 — harmonic mean of precision and recall. Balances both.

Choose based on cost: spam filter needs high precision (don't block real emails); cancer screening needs high recall (don't miss cases).

from sklearn.metrics import precision_score, recall_score, f1_score
print(f1_score(y_true, y_pred))

How do you read a confusion matrix?

A table comparing predicted vs actual labels:

                 Predicted
              Pos    Neg
Actual Pos    TP     FN
       Neg    FP     TN
  • TP — correctly predicted positive
  • FP — false alarm (Type I error)
  • FN — missed positive (Type II error)
  • TN — correctly predicted negative

All classification metrics derive from these four counts.

from sklearn.metrics import confusion_matrix
print(confusion_matrix(y_true, y_pred))

What is ROC-AUC?

ROC curve — plots True Positive Rate (recall) vs False Positive Rate at various classification thresholds.

AUC (Area Under Curve) — single number summarising ROC. 1.0 = perfect; 0.5 = random guessing.

Threshold-independent metric — useful for comparing models regardless of the chosen decision threshold. Good for imbalanced datasets when you haven't decided on a threshold yet.

from sklearn.metrics import roc_auc_score
auc = roc_auc_score(y_true, y_prob)  # needs probability scores, not class labels

How do you tune hyperparameters?

Hyperparameters are set before training (learning rate, tree depth, number of layers) — not learned from data.

Methods:

  • Grid search — try all combinations from a predefined grid. Exhaustive but expensive.
  • Random search — sample random combinations. Often finds good configs faster than grid.
  • Bayesian optimisation — model the objective function; sample intelligently (Optuna, Hyperopt).

Always tune on validation set, evaluate final config on test set.

from sklearn.model_selection import GridSearchCV
grid = GridSearchCV(model, param_grid, cv=5, scoring='f1')
grid.fit(X_train, y_train)
print(grid.best_params_)

What is transfer learning?

Using a model pre-trained on a large dataset as a starting point for your specific task. Instead of training from scratch, you fine-tune the last layers (or the whole model) on your smaller dataset.

Examples: ImageNet-pretrained ResNet for medical imaging; BERT/GPT for NLP tasks; Whisper for speech.

Saves data, compute, and time — almost always better than training from scratch on small datasets.

from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained(
  "bert-base-uncased", num_labels=3
)

What is data leakage and how do you prevent it?

When information from outside the training set leaks into training, giving unrealistically good evaluation metrics that don't hold in production.

Common causes:

  • Fitting scaler/encoder on full dataset before splitting
  • Using future data to predict the past (time series)
  • Target leakage — a feature that directly encodes the label
  • Duplicate records across train and test

Fix: split first, then preprocess. Use pipelines. Audit features for leakage.

from sklearn.pipeline import Pipeline
pipe = Pipeline([
  ('scaler', StandardScaler()),
  ('model', LogisticRegression())
])
pipe.fit(X_train, y_train)  # scaler fits only on train

How do you handle imbalanced datasets?

  • Right metrics — use F1, precision-recall, AUC — not accuracy.
  • Class weights — penalise misclassifying the minority class more (class_weight='balanced').
  • Oversampling — duplicate minority samples (SMOTE generates synthetic ones).
  • Undersampling — reduce majority class (loses data).
  • Threshold tuning — adjust decision threshold to favour recall or precision.
  • Ensemble methods — combine models trained on balanced subsets.
from sklearn.utils.class_weight import compute_class_weight
weights = compute_class_weight('balanced', classes=np.unique(y), y=y)

What are ensemble methods?

Combine multiple models for better performance than any single model:

  • Bagging — train many models on random subsets, average predictions. Example: Random Forest (bagged decision trees).
  • Boosting — train models sequentially, each correcting previous errors. Examples: XGBoost, LightGBM, CatBoost.
  • Stacking — train a meta-model on base model predictions.

Gradient boosted trees dominate tabular data competitions and production ML.

import xgboost as xgb
model = xgb.XGBClassifier(n_estimators=100, max_depth=6)
model.fit(X_train, y_train)

L1 vs L2 regularization.

Both add a penalty term to the loss function to prevent large weights:

  • L1 (Lasso) — penalty = λΣ|w|. Drives some weights to exactly zero → feature selection. Sparse models.
  • L2 (Ridge) — penalty = λΣw². Shrinks weights toward zero but rarely to exactly zero. Smoother models.
  • Elastic Net — combines L1 + L2.

In neural nets, L2 regularisation is called weight decay.

from sklearn.linear_model import Lasso, Ridge
lasso = Lasso(alpha=0.1)   # L1
ridge = Ridge(alpha=1.0)   # L2

What is batch normalization?

Normalises the inputs to each layer during training — zero mean, unit variance within each mini-batch. Reduces internal covariate shift (changing input distributions as earlier layers update).

Benefits: faster training, allows higher learning rates, acts as mild regularisation. Standard in CNNs and feedforward networks. Layer Normalisation is preferred in transformers (normalises across features, not batch).

import torch.nn as nn
layer = nn.Sequential(
  nn.Linear(256, 128),
  nn.BatchNorm1d(128),
  nn.ReLU()
)

What is MLOps and why does it matter?

MLOps applies DevOps principles to ML: reproducible training, automated deployment, monitoring, and retraining.

Key practices:

  • Version everything — data, code, model weights, configs (MLflow, DVC).
  • CI/CD for models — automated testing, validation gates before deployment.
  • Model serving — REST/gRPC endpoints, batch inference, edge deployment.
  • Monitoring — track prediction drift, data drift, latency, error rates.
  • Retraining pipelines — trigger when performance degrades or new data arrives.