Skip to content
Tutorials Center
My learning

Topics

Lesson 74 of 76 ยท Practice and Review

Interview Questions: Machine Learning and Deep Learning

Machine learning and deep learning interview questions with short answers and runnable demos: imbalance, pruning, boosting, losses, SVMs, LSTMs, GANs and LLMs.

The main interview page has the machine learning basics: bias and variance, leakage, cross-validation, precision and recall, regularization. This page covers the next layer of questions, the ones an interviewer asks once you have answered the basics well: how to fix imbalanced data, why trees are pruned, how boosting libraries differ, what the loss functions do, and how neural networks, from the plain feed-forward network to GANs and large language models, are built and why they train the way they do.

Every answer follows the same shape: the direct answer, the reason, and one trade-off. The runnable examples use NumPy and scikit-learn so that you can see the numbers behind the claims. Neural-network libraries do not run in the browser, so for the deep learning part the demos rebuild the key mechanism (activations, vanishing gradients, convolution) in plain NumPy.

Data problems

1. What are upsampling and downsampling? What are SMOTE and cluster-based undersampling?

Both rebalance a dataset where one class is rare. Upsampling (oversampling) adds minority examples; downsampling (undersampling) removes majority examples. Plain random duplication works but makes the model memorize the repeated rows. SMOTE (Synthetic Minority Over-sampling Technique) creates new minority points: pick a minority example, pick one of its nearest minority neighbours and place a point at a random spot on the line between them. Cluster-based undersampling clusters the majority class and keeps one representative per cluster (for example the point nearest each centre), so the smaller majority set still covers the whole space instead of being a random thin slice. Always resample only the training set, and judge with precision, recall and PR curves, never accuracy. Resampling shifts the predicted probabilities, so recalibrate if you need real probabilities. See also Evaluating Models.

2. What is the curse of dimensionality?

As the number of features grows, the space grows exponentially and your data becomes sparse: ten bins per axis need 10 cells in one dimension but ten billion in ten. Distances also concentrate: the nearest and the farthest neighbour end up almost equally far away, so distance-based methods (k-NN, k-means) lose their meaning, and models need far more data to avoid overfitting. Remedies: select features, reduce dimensions (PCA), use regularization, collect more data, or choose models that cope better. See Dimensionality Reduction.

Python Resampling an imbalanced set, and distance concentration

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both resampling methods trade precision for recall: the model now finds far more of the rare class but raises more false alarms. Which side you want depends on what a miss costs. In the second block the ratio of nearest to farthest distance climbs from about 0.05 towards 1 as the dimension grows: "near" stops meaning anything.

Trees, boosting and probabilistic models

3. What is pruning in decision trees?

An unrestricted tree keeps splitting until every leaf is pure, so it memorizes noise. Pruning shrinks it to generalize better. Pre-pruning (early stopping) stops growth by limits such as max_depth, min_samples_leaf or a minimum gain per split. Post-pruning grows the full tree and then removes branches whose gain does not justify their size; scikit-learn's version is cost-complexity pruning, controlled by ccp_alpha (a larger value cuts more). Pre-pruning is cheap but can stop too early (a weak split may enable a strong one below it); post-pruning is more thorough but slower. Choose the setting by cross-validation. See Tree Models and Ensembles.

4. How does gradient boosting work, and how do XGBoost and CatBoost differ?

Gradient boosting builds trees one after another; each new small tree is fitted to the errors (the negative gradient of the loss) of the ensemble so far, and its output is added with a small learning rate. It reduces bias, and too many trees overfit, so use early stopping on a validation set. XGBoost is a fast, regularized implementation (L1 and L2 penalties on leaf weights, second-order gradients, built-in missing-value handling, parallel split search). CatBoost adds native handling of categorical features using ordered target statistics (to avoid target leakage) and symmetric (oblivious) trees, which are fast to predict and regularize well, with strong defaults that need little tuning. LightGBM is the third common choice (leaf-wise growth, very fast on large data).

Python
# Read-only: these libraries are not available in the browser runner
from xgboost import XGBClassifier
model = XGBClassifier(n_estimators=500, learning_rate=0.05, max_depth=4,
                      subsample=0.8, colsample_bytree=0.8, reg_lambda=1.0,
                      early_stopping_rounds=30, eval_metric="logloss")
model.fit(X_train, y_train, eval_set=[(X_valid, y_valid)])

from catboost import CatBoostClassifier
cat = CatBoostClassifier(iterations=500, learning_rate=0.05, depth=6,
                         cat_features=["city", "plan"], verbose=0)
cat.fit(X_train, y_train, eval_set=(X_valid, y_valid), early_stopping_rounds=30)
Python Pre-pruning, post-pruning and boosting rounds

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The unpruned tree has about 150 leaves and a perfect training score, but it is the worst on the test set. Every pruned tree is smaller and scores better on new data.

5. What is a confusion matrix?

A table of predictions against the truth. For a binary problem it has four cells: true positives (said yes, was yes), false positives (said yes, was no), false negatives (said no, was yes) and true negatives. From it come precision TP / (TP + FP), recall TP / (TP + FN), F1 (their harmonic mean) and accuracy (TP + TN) / all. It shows which mistakes a model makes, which accuracy hides on imbalanced data. For several classes it becomes an N by N grid whose off-diagonal cells show which classes get confused.

6. What is Laplace smoothing and why does Naive Bayes need it?

Naive Bayes multiplies word probabilities. If a word never appeared with a class in training, its estimated probability is 0, and the whole product becomes 0 no matter how strongly the other words point to that class. Add-one (Laplace) smoothing adds a pseudo-count alpha (usually 1) to every word count in every class: P(word | class) = (count + alpha) / (total words in class + alpha * vocabulary size). No probability is ever zero. A smaller alpha trusts the data more; a larger one pulls estimates towards uniform.

7. How does the EM algorithm work for clustering?

EM fits a Gaussian mixture model: the data are assumed to come from several bell curves, but nobody told us which point belongs to which. It alternates two steps. E-step: with the current means, spreads and weights, compute for each point the probability that each component produced it (a soft assignment). M-step: recompute each component's weight, mean and covariance as weighted averages using those probabilities. Each round never lowers the likelihood, so it converges, but possibly to a local optimum, so use several starts. Unlike k-means it gives soft memberships and elliptical clusters; k-means is the special case with equal spherical clusters and hard assignments.

Python Confusion matrix, Laplace smoothing and a Gaussian mixture

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Without smoothing, "win" has probability 0 in the ham class, so one unseen word would veto a class completely; with alpha=1 it gets a small positive probability. The mixture recovers the planted components: weights about 0.4 and 0.6, means near 0 and 6, standard deviations near 1 and 1.5. The point x = 3, lying between them, gets a soft split.

Loss functions, margins and validation

8. Which loss functions should you know, and when is each used?
  • Mean squared error punishes big errors heavily (squares), so one outlier can dominate. Mean absolute error is robust but has a kink at zero.
  • Huber loss is quadratic for small errors and linear for large ones (the switch point is delta): smooth like MSE near the target, robust like MAE for outliers.
  • Cross-entropy (log loss) is the standard for classification with probabilities: -[y log p + (1 - y) log(1 - p)]. It punishes confident wrong answers extremely hard and is the quantity logistic regression and most neural classifiers minimize.
  • Hinge loss max(0, 1 - y * score) with labels -1 and +1 belongs to SVMs: correct predictions beyond the margin cost nothing, so only points near or inside the margin matter.
  • KL divergence sum P log(P / Q) measures how far distribution Q is from P. It is not symmetric and not a distance. Cross-entropy equals the entropy of P plus KL(P || Q), so minimizing cross-entropy minimizes KL. It also appears in variational autoencoders and in distillation.
9. What are kernels and a hyperplane in an SVM?

A support vector machine looks for the hyperplane (a line in 2D, a plane in 3D, a flat surface in more dimensions) that separates the classes with the largest margin. Only the closest points, the support vectors, determine it. When classes are not linearly separable, a kernel function computes similarity as if the points had been mapped into a much higher-dimensional space where a flat separator exists, without computing that mapping (the kernel trick). Common kernels: linear, polynomial and RBF (Gaussian). The parameter C trades a wide margin against training errors, and gamma in the RBF kernel sets how local the influence of each point is.

10. What is shuffle-split cross-validation?

ShuffleSplit repeats a random train/test split n times. Unlike k-fold, test sets may overlap between repeats, and you choose the train and test proportions and the number of repeats independently of each other. It is handy for large datasets or when you want many cheap evaluations, and StratifiedShuffleSplit keeps class proportions. Never use it for time series (use forward-chaining splits) or for grouped data where rows of one person must stay together (use group splits). See Train, Test and Validation.

11. What is mini-batch gradient descent?

Gradient descent updates parameters against the gradient of the loss. Batch descent uses all rows for each step: smooth but slow and memory-hungry. Stochastic descent uses one row: very noisy but many updates. Mini-batch uses a small group (32 to 512 rows): a good compromise, because it uses vectorized hardware efficiently and the batch noise is small enough to converge yet large enough to escape shallow traps. One pass through all data is an epoch. The batch size and learning rate interact; larger batches usually tolerate larger learning rates.

Python Losses, SVM kernels, shuffle split and batch sizes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The squared error of 225 versus a Huber loss of about 7.4 shows how one outlier (40 against a prediction of 10) dominates MSE. A linear kernel cannot separate two concentric rings (about 55 percent), while the RBF kernel does it perfectly. After 20 epochs the full-batch run has only made 20 updates and is still far from the true weights [2, -1, 0.5], while mini-batches of 32 (640 updates) have nearly reached them.

Neural networks

12. What is an artificial neural network?

A stack of layers of simple units. Each unit takes a weighted sum of its inputs, adds a bias and passes the result through a non-linear activation function. Layers are chained: input, one or more hidden layers, output. Training adjusts weights and biases: a forward pass computes predictions and the loss, backpropagation applies the chain rule to find the gradient of the loss with respect to every weight, and an optimizer (SGD, Adam) takes a step. With enough units a network can approximate almost any smooth function, but it needs lots of data, tuning and regularization (dropout, weight decay, early stopping).

13. What are activation functions and why do we need them?

Without a non-linear activation, any number of layers collapses into a single linear map, so the network could only draw straight boundaries. Common choices: sigmoid squashes to (0, 1) (output for probabilities, but its gradient is at most 0.25 and it saturates); tanh squashes to (-1, 1) and is zero-centred; ReLU max(0, x) is cheap, does not saturate for positive values and is the default in hidden layers (its risk is "dead" units stuck at 0, which Leaky ReLU softens); softmax turns a vector of scores into probabilities that sum to 1 and is used on the output of multi-class classifiers.

14. What are vanishing and exploding gradients?

Backpropagation multiplies one factor per layer (or per time step). If those factors are mostly smaller than 1 (a sigmoid's derivative is at most 0.25), the product shrinks exponentially and early layers barely learn: vanishing. If they are larger than 1 the product grows and updates blow up to huge numbers or NaN: exploding. Fixes: ReLU-family activations, careful initialization (Xavier, He), batch or layer normalization, residual (skip) connections, gated units such as LSTM, and gradient clipping for exploding gradients.

Python Activation functions and vanishing gradients in NumPy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With weights a little too small the gradient practically disappears after 30 layers; a little too large and it explodes. This is why initialization is scaled to the layer width.

15. Explain the convolution operation in a CNN.

A convolutional layer slides a small filter (kernel, for example 3 by 3) across the image. At every position it multiplies the overlapping pixels by the filter's weights and sums them, producing one number of the feature map. The same weights are reused at every position (weight sharing), so the layer detects a pattern, such as a vertical edge, wherever it appears, with far fewer parameters than a fully connected layer. Output size is (n - k + 2p) / s + 1 for input size n, kernel k, padding p and stride s. A layer learns many filters; early ones find edges and textures, later ones combine them into parts and objects. Pooling then downsamples the maps. The sketch below applies an edge filter by hand.

Python A convolution by hand

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two middle columns light up exactly where the dark-to-bright edge sits, and everything else is zero.

16. What are the differences between RNN, LSTM and GRU, and how does an LSTM fix vanishing gradients?

A recurrent neural network reads a sequence step by step, carrying a hidden state that summarizes the past. Training unrolls it through time, and the repeated multiplication by the same weights makes gradients vanish over long sequences, so a plain RNN forgets distant context. An LSTM (long short-term memory) adds a separate cell state, a conveyor belt that runs through time, changed only by small additive updates controlled by three gates (sigmoid outputs between 0 and 1): the forget gate decides what to erase, the input gate what to write and the output gate what to expose. Because the cell state is updated by addition and scaled by a gate that can stay close to 1, gradients can flow back through many steps without shrinking. A GRU merges the gates into two (update and reset) and drops the separate cell state: fewer parameters, faster training, often similar accuracy. Today transformers have replaced most recurrent models for text, but the questions remain popular.

Generative models and large language models

17. What is the difference between generative and discriminative models?

A discriminative model learns the boundary or the conditional probability P(y | x): given the input, which class? Logistic regression, SVMs, trees and most neural classifiers belong here; they usually give better accuracy on pure prediction. A generative model learns how the data themselves arise, P(x, y) or P(x), so it can also create new samples, handle missing inputs and detect outliers. Naive Bayes, Gaussian mixtures, hidden Markov models, GANs, VAEs and language models are generative.

18. What is a Seq2Seq model?

A sequence-to-sequence model maps an input sequence to an output sequence of a different length, as in translation, summarization or speech recognition. An encoder reads the input and compresses it into a representation; a decoder produces the output one token at a time, feeding back its previous output. Early versions used RNNs and squeezed everything into one fixed vector, which lost information on long inputs; attention fixed this by letting the decoder look back at every encoder state, and the transformer is the model built entirely on attention.

19. What is generative AI?

AI systems that produce new content (text, images, audio, code, video) rather than only labelling or scoring existing data. They learn the patterns of a huge training set and sample new items from them: large language models, diffusion models for images, GANs and VAEs. Typical uses: drafting and summarizing, code assistance, synthetic data, design. Typical risks: confident but false statements ("hallucinations"), biased or copyrighted training content, privacy leaks and misuse, so outputs need evaluation and human review. See Ethics and Fairness.

20. What is the difference between GANs and VAEs?

A GAN (generative adversarial network) trains two networks against each other: a generator turns random noise into fake samples and a discriminator tries to tell fake from real; each improves because of the other. GAN images are sharp, but training is unstable and can suffer mode collapse (the generator keeps producing a few outputs). A VAE (variational autoencoder) has an encoder that maps an input to a distribution in a smooth latent space and a decoder that reconstructs it; the loss is reconstruction error plus a KL divergence term that keeps the latent space close to a standard normal. Training is stable and the latent space is useful (you can interpolate), but samples tend to be blurrier. Diffusion models have overtaken both for images.

21. What are large language models, and what does fine-tuning mean?

An LLM is a very large transformer trained on huge amounts of text to predict the next token; that single objective, at scale, produces models that can answer, summarize, translate and write code. Fine-tuning continues training a pre-trained model on a smaller, task-specific dataset so that it follows instructions or fits a domain: full fine-tuning updates all weights; parameter-efficient methods such as LoRA train small added matrices and leave the base weights frozen, which is cheap. Alternatives that need no training are prompting, few-shot examples and retrieval-augmented generation, which puts relevant documents into the prompt. Choose them in that order of effort: prompt first, retrieval next, fine-tune when the behaviour or style must be baked in.

Python
# Read-only sketch of a LoRA fine-tune with Hugging Face libraries
from peft import LoraConfig, get_peft_model
from transformers import AutoModelForCausalLM

base = AutoModelForCausalLM.from_pretrained("my-base-model")
lora = LoraConfig(r=8, lora_alpha=16, target_modules=["q_proj", "v_proj"], lora_dropout=0.05)
model = get_peft_model(base, lora)
model.print_trainable_parameters()   # typically well under 1 percent of all weights

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why should resampling such as SMOTE be applied only to the training data?

    The test set must look like the real world. Adding synthetic or removed rows to it, or creating synthetic points from test neighbours, gives a misleading evaluation.
  2. 2. What does a larger ccp_alpha do to a scikit-learn decision tree?

    Cost-complexity pruning charges a price per leaf. A larger alpha makes extra leaves more expensive, so more are removed.
  3. 3. Why does Naive Bayes use Laplace smoothing?

    A single zero in the product of word probabilities would zero out the whole class score. Adding a pseudo-count keeps every probability positive.
  4. 4. Which statement about the LSTM cell state is correct?

    The additive path, scaled by a forget gate that can stay near 1, is the reason an LSTM keeps long-range information that a plain RNN loses.
  5. 5. In a VAE, what is the role of the KL divergence term?

    Reconstruction loss alone would let the encoder scatter codes anywhere. The KL term regularizes them towards a standard normal, which makes sampling and interpolation work.

Summary

  • Rebalance only the training set (SMOTE, cluster-based undersampling) and judge with precision and recall; high dimensions make data sparse and distances meaningless.
  • Pre-pruning limits growth, post-pruning (cost-complexity) cuts a full tree; boosting fits each tree to the previous errors, XGBoost adds regularization, CatBoost handles categories natively.
  • A confusion matrix shows the type of errors; Laplace smoothing prevents zero probabilities; EM alternates soft assignment and parameter updates.
  • Know the losses: MSE, MAE, Huber (robust), cross-entropy (classification), hinge (SVM), KL divergence (distribution distance).
  • SVMs maximize the margin and use kernels for non-linear boundaries; ShuffleSplit repeats random splits; mini-batches balance speed and noise.
  • Networks need non-linear activations; sigmoid and bad initialization cause vanishing or exploding gradients; CNNs share small filters; LSTM and GRU use gates to remember.
  • Discriminative models predict P(y | x), generative models learn the data distribution; GANs are adversarial and sharp, VAEs are stable and smooth; LLMs are adapted by prompting, retrieval or fine-tuning.

Next: Coding Interview Questions, Python, SQL and NumPy problems to practise by writing code.