93 Machine Learning & DS interview questions with detailed answers, sorted from basic to advanced. Free to practice, with progress saved automatically.
Machine learning builds models that learn patterns from data in order to predict outcomes or uncover structure, backed by statistics for data science work. The bank covers supervised and unsupervised learning, model evaluation and validation, the bias-variance tradeoff, regularization, feature engineering, preprocessing, ensembles, training, inference, and LLM fundamentals.
and 30 more topics.
A model's expected test error decomposes into three parts: Bias², Variance, and irreducible error (noise σ² that cannot be removed): E[(y − f̂(x))²] = Bias(f̂(x))² + Var(f̂(x)) + σ² - Bias — error from a model too simple to capture the true relationship. High bias…
Overfitting is when a model memorizes noise and quirks of the training set instead of the general pattern — scoring very high on training data but poorly on new data. Telltale sign: a large gap between training error and validation/test error. (The opposite, underfitting, is…
For binary classification, a confusion matrix is a 2×2 table matching true vs predicted labels, with 4 cells: TP (correct positive), TN (correct negative), FP (false positive — predicted positive but actually negative), FN (false negative — a missed positive). - Precision = TP /…
A probabilistic classifier needs a threshold to turn probabilities into 0/1 labels. The ROC curve plots the relationship between two quantities as the threshold sweeps from 0 to 1: - Y-axis: TPR = Recall = TP / (TP + FN). - X-axis: FPR = FP…
Cross-validation estimates generalization by splitting data and training/testing on different portions, giving a less luck-dependent estimate than a single split. - Hold-out — one split into train/test (e.g. 80/20). Fast, but the estimate has high variance: results depend heavily on where the split lands. -…
Feature engineering is creating and transforming features so the model learns more easily: encoding categoricals (one-hot, target encoding), scaling numerics, building interaction or polynomial features, extracting parts from datetimes, log-transforming skewed distributions, or crafting domain-knowledge features. It is often the highest-impact step for quality in…
Imbalanced data (e.g. 99% negative, 1% positive as in fraud detection) makes accuracy misleading — a model that always predicts the majority still hits 99%. Address both the metric and the data: - Pick suitable metrics — precision, recall, F1, PR-AUC instead of accuracy. -…
Split data into three parts, each with a distinct role: - Train — used to fit the model parameters. - Validation — used to select models and tune hyperparameters. Because you make decisions from it, the validation score is already "contaminated" with information. - Test…
Gradient descent updates parameters in the direction opposite the loss gradient. The three variants differ in how much data each update uses: - Batch GD — computes the gradient over the entire dataset before each single update. Accurate direction, smooth convergence, but each step is…
A classifier usually outputs a probability, then compares it to a threshold to assign a positive/negative label. The default 0.5 threshold is rarely optimal. Tradeoff: - Lower the threshold → label positive more readily → recall rises (catch more positives) but precision falls (more false…
Four common metrics for evaluating regression models (with y the true value and ŷ the prediction): - MSE (Mean Squared Error) = mean of (y − ŷ)². Penalizes large errors heavily (squared), sensitive to outliers. Its units are the target squared, so it is hard…
Linear regression models the relationship between a continuous target y and features X as a linear function: y = β₀ + β₁x₁ + … + βₚxₚ + ε. The coefficients β are usually estimated by ordinary least squares (OLS) — minimizing the sum of squared…
Because it estimates the probability that a sample belongs to the positive class, then compares it to a threshold to produce a label. Internally it still computes a linear combination z = β₀ + βᵀx like linear regression, but passes it through the sigmoid/logistic function…
K-Means partitions data into k clusters by iterating two steps: 1. Assign: each point to its nearest centroid by Euclidean distance. 2. Update: each new centroid = the mean of the points in its cluster. Repeat until the assignment stabilizes. The objective is to minimize…
KNN predicts by finding the k nearest training points to the query sample (by a distance metric, usually Euclidean), then taking a majority vote (classification) or an average (regression) over them. It is a "lazy" learner because it builds no model at training time —…
A decision tree splits the data recursively on features. At each node it picks the split that makes the child branches as label-pure as possible, measured by an impurity metric: - Gini impurity: 1 − Σ pᵢ² — the probability of misclassifying if you labeled…
- Supervised: learns from labeled data (each input paired with the correct output). The goal is to learn a mapping to predict labels for new data. It covers classification (discrete labels) and regression (continuous values). Examples: linear/logistic regression, SVM, decision trees. - Unsupervised: the data…
Naive Bayes is a probabilistic classifier based on Bayes' theorem: it picks the class y with the highest posterior probability, P(y x) ∝ P(y) · Π P(xⱼ y). The "naive" assumption is conditional independence: the features are independent of each other given the class. This…
Both combine multiple base models to predict better than any single one, but they differ in how they aggregate: - Voting: merges predictions with a fixed, non-learned rule. - Hard voting: take the label by majority vote. - Soft voting: average the predicted probabilities and…
- K-Means: partitions data into a preset k clusters around centroids; fast and scalable. But it assumes spherical, similarly sized clusters, is sensitive to outliers, and requires choosing k upfront. - DBSCAN (density-based): groups high-density points (at least minPts within radius ε) and treats sparse-region…
The CLT states that for a large enough sample size n, the distribution of the sample mean (x̄) is approximately normal, regardless of the shape of the underlying population — provided observations are independent, identically distributed, and the population has finite variance. - The mean…
A p-value is the probability of observing data at least as extreme as what you got, assuming the null hypothesis (H0) is true. A small p-value means the data is unlikely under H0 → evidence against H0. Compare it to the significance level α: if…
Correlation measures the strength and direction of a linear relationship between two variables. Correlation does not imply causation: a link can arise from a confounder, reverse causation, or coincidence. Only a controlled / randomized experiment can establish causation. Covariance vs correlation coefficient: - Covariance captures…
Hypothesis testing is a procedure that uses sample data to decide between two competing claims: 1. State the hypotheses. H0 (null) describes the default / no-effect state; H1 (alternative) is what you want to demonstrate (one- or two-sided). 2. Choose a significance level α (commonly…
The normal (Gaussian) distribution is a symmetric bell-shaped distribution fully described by its mean μ and standard deviation σ, written N(μ, σ²). The 68–95–99.7 (empirical) rule: - ~68% of data falls within μ ± 1σ - ~95% within μ ± 2σ - ~99.7% within μ…
A confidence interval (CI) is a range estimate for a population parameter computed from sample data. For a mean: x̄ ± z · (σ/√n) (use t with the sample standard deviation s when σ is unknown). z is the critical value for the confidence level…
Conditional probability P(AB) is the probability of A given that B occurred: P(AB) = P(A∩B) / P(B). Bayes' theorem lets you "flip" the condition, updating beliefs after seeing evidence: P(AB) = P(BA) · P(A) / P(B) - P(A): the prior (belief before). - P(BA): the…
Probability sampling methods (each unit has a known chance of selection): - Simple random — every individual has an equal chance. - Stratified — split the population into subgroups (strata), then sample within each; ensures subgroups are represented. - Cluster — split into clusters, randomly…
Measures of central tendency: - Mean (average) — sum divided by count; sensitive to outliers. - Median — the middle value when sorted; robust to outliers and skew. - Mode — the most frequent value; works for categorical data too. For skewed data (like income),…
Both describe the shape of a distribution beyond its mean and variance. Skewness measures asymmetry: - Right / positive skew — a long right tail, mean median (e.g. income, response times). - Left / negative skew — a long left tail, mean < median. -…
All are gradient-descent variants that differ in how they use gradient history to set the step. - SGD + momentum: accumulates a decayed moving average of gradients (a "velocity") → smooths updates, speeds up along consistent directions, dampens oscillation. Uses a single global learning rate…
Both are regularization techniques — reducing overfitting without extra data. - Dropout: during training, each forward pass randomly drops (zeroes out) each neuron with probability p. The network cannot rely on a few co-adapted units → this effectively trains an ensemble of subnetworks that share…
A CNN processes grid-like data (images) via local connectivity and weight sharing. - Kernel / filter: a small weight matrix slid over the input; at each position it computes a dot product → detecting a local pattern (edge, texture). The weights are learned. - Convolution:…
An activation function injects non-linearity into the network. Without it, stacked linear layers collapse into a single linear map. - Sigmoid 1/(1+e^(-x)): output in (0, 1). Use it for a binary probability at the output. In hidden layers it saturates easily: when the input is…
Backpropagation is the algorithm that computes the gradient of the loss with respect to every weight efficiently, so gradient descent can update them. Two passes: 1. Forward pass: feed the input through the network, computing each layer's output and the final loss. 2. Backward pass:…
The loss function defines what "wrong" means and produces the gradients that drive learning. - MSE (mean squared error) mean((y − ŷ)²): for regression (continuous targets). Penalizes large errors quadratically; sensitive to outliers. Pairs with a linear output. - Cross-entropy (log loss): for classification. Measures…
- Epoch: one full pass over the entire training set. - Batch (mini-batch): the group of samples processed before one weight update; batchsize sets its size. - Iteration: one weight update = one processed batch. iterations per epoch = ceil(N / batchsize). Example: 10,000 samples,…
In RL, the agent must balance: - Exploitation — pick the best known action to maximize immediate reward. - Exploration — try uncertain actions to gather information that may reveal a better choice later. Pure exploitation → getting stuck in a local optimum (never trying…
- Reward r — a scalar signal the environment returns each step, measuring the immediate goodness of an action. The agent aims to maximize the cumulative reward (return), not any single-step reward. - Policy π — the agent strategy, mapping states → actions. It can…
Content-based filtering — recommends based on item features and the user own preference profile. If you like sci-fi movies, it suggests other sci-fi movies. - Pros: needs no data from other users; handles new items (as long as they have features); easy to explain. -…
First understand the missingness mechanism: MCAR (missing completely at random), MAR (depends on other observed variables), MNAR (depends on the missing value itself) — this determines whether a strategy introduces bias. Main strategies: - Deletion — drop rows (listwise) or columns. Simple but loses data;…
Many models accept only numeric input, so categorical variables need encoding. - One-hot encoding — each value becomes its own binary column. It imposes no fake ordering, fitting nominal variables and linear models. Downside: a dimensionality blow-up at high cardinality (many distinct values). - Label…
Scaling brings numeric features onto a comparable range so no feature dominates just because its units are large. - Normalization (Min-Max) — squeeze values into a fixed range, usually [0, 1], using x′ = (x − min) / (max − min). It preserves the distribution…
The difference is whether the model assumes a fixed functional form up front (with a finite number of parameters that does not grow with the data) or not. Parametric — assume a functional form and learn a fixed number of parameters. Examples: linear/logistic regression, Naive…
A learning curve plots training error and validation error against the number of training samples. The shape of the two curves tells you where the problem is. High bias (underfitting): both curves plateau at a high error and sit close together. Adding data barely helps…
A baseline is the reference point that tells you whether a complex model actually adds value. Without one, an AUC of 0.85 means nothing. Baselines worth running first: - Constant / majority: always predict the most frequent class (classification) or the mean (regression). In scikit-learn:…
Scaling matters for any model where feature magnitude feeds directly into the objective function or into a distance. Needs scaling: - Distance-based models: KNN, K-Means, SVM with an RBF kernel. An income feature in millions will dominate age in a Euclidean distance. - Gradient-descent models:…
Because a model that predicts everything as negative already reaches 99.5% accuracy while catching zero fraudulent transactions. On imbalanced data, accuracy is dominated by the majority class and does not reflect performance on the class you care about. Metrics to use instead: - Recall (sensitivity):…
The difference is in the denominator of the x-axis. - ROC plots TPR against FPR, where FPR = FP / (FP + TN). When the negative class is huge, TN is enormous, so FPR barely moves even as FP grows by thousands. The ROC curve…
Leakage is information that would not exist at real prediction time slipping into training. The classic symptom: unusually high offline scores that collapse in production. Common forms: 1. Preprocessing before splitting. Calling scaler.fit() or imputing on the full dataset and only then doing traintestsplit →…
0.5 is just the convention baked into predict(), not the result of any optimization. The right threshold depends on the cost of the two error types and the class ratio — neither of which the model knows. How to choose it properly: 1. Build a…
When to do nothing: imbalance is not a defect by itself. If you use an appropriate metric (PR-AUC, recall at a fixed precision) and choose your own threshold, a probabilistic model trained on the raw distribution is often enough. In many cases lowering the threshold…
Because random k-fold shuffles away the time order: the model trains on June data and then predicts March. That is temporal leakage, and the cross-validation score will be far better than what the model achieves when actually forecasting an unseen future. The right approach —…
Three families of causes, distinguished by what actually changed: 1. Data drift (covariate shift) — the input distribution P(X) changed while P(yX) held. For example the customer base expanded to a new region, or an app redesign left a feature permanently empty. 2. Concept drift…
No. The first question is whether the outlier is a data error or a genuine rare event. - age = 200, price = -1: entry or unit errors → fix or drop. - A 5-billion-VND transaction in a fraud dataset: that is exactly what you…
EDA is reading and understanding the data before doing anything with it: what it looks like, what is wrong with it, which variables relate to the target. The goal is to know what you are holding, not yet to build a model. The usual order:…
Pick the chart by the question you want to answer, not by taste: comparing groups, trend over time, distribution, relationship between two variables, or share of a total. Question Chart Example --------- Which group is bigger? Bar (vertical; horizontal when labels are long) Revenue by…
Modern OCR does two main jobs: text detection finds the regions that contain text, then text recognition reads the character sequence in each region. Around them sit image preprocessing and text post-processing. 1. Preprocessing: grayscale, denoise, binarise (Otsu), deskew, and fix perspective when a document…
IoU (Intersection over Union) measures how much two boxes overlap: the intersection area divided by the union area, from 0 (no contact) to 1 (identical). NMS uses IoU to remove duplicates: a detector usually fires many boxes around one object, and NMS keeps the highest-scoring…
With logits z, probabilities p = softmax(z) and one-hot label y, the derivative of cross-entropy with respect to the logits is ∂L/∂zi = pi − yi: prediction minus label. It is that clean because the derivative of log cancels exactly the pk produced by the…
Data augmentation creates extra variants of training images with transformations that do not change the label: a horizontally flipped cat is still a cat. The model sees more forms of the same object, so it memorises fewer incidental details of the training set, i.e. less…
The three concepts nest like circles: AI ⊃ Machine Learning ⊃ Deep Learning. AI is the goal (machines doing tasks that need human intelligence), ML is one way to reach it (learning patterns from data instead of hand-writing rules), and DL is a branch of…
Logistic Regression assumes the label y ∈ {0, 1} follows a Bernoulli distribution with probability p = σ(wᵀx + b). The negative log-likelihood of that distribution is exactly log loss (binary cross-entropy), and its gradient simplifies to Σ (pi − yi) xi. Step 1, the…
Choose by purpose: PCA for real dimensionality reduction before a model (linear, fast, applies to new data), t-SNE almost only for plotting data in 2D/3D so humans can see clusters, and UMAP in between: non-linear like t-SNE but faster and usable as a preprocessing step…