Skip to content

PUBLIC QUESTION PREVIEW

ML & AI interview questions

Machine learning fundamentals, neural networks, model evaluation, LLMs, and MLOps.

Showing 12 of 52 source questions. Sample answers are study material, not employer-specific hiring guidance.

How to work through this preview

Attempt each question before reading its answer. For a concept, give a definition and a concrete example. For code, state constraints, walk through a small input, and check complexity. For design, separate requirements from assumptions and explain failure handling. Use real situations for behavioral answers.

QUESTION 1 / easy / conceptual

What is the difference between supervised, unsupervised, and reinforcement learning?

Sample answer

Supervised Learning: learn from labeled examples (input → known output). - Training data: (email text, spam/not-spam label) - Goal: predict label for new inputs - Algorithms: linear regression, logistic regression, decision trees, SVM, neural networks - Examples: spam detection, house price prediction, image classification Unsupervised Learning: find patterns in data without labels. - Training data: just inputs, no labels - Goal: discover structure, clusters, representations - Algorithms: K-means clustering, PCA, autoencoders, GANs - Examples: customer segmentation, anomaly detection, dimensionality reduction Reinforcement Learning (RL): agent learns by interacting with an environment and receiving rewards/penalties. - No labeled dataset — agent explores and learns from outcomes - Key components: Agent, Environment, State, Action, Reward - Goal: maximize cumulative reward over time - Algorithms: Q-learning, PPO, A3C - Examples: AlphaGo, game-playing AI, robot control, ad bidding Self-supervised Learning (growing field): generate labels from the data itself. - Examples: BERT (predict masked words), GPT (predict next word), contrastive learning (SimCLR) - No human labeling needed — scales to enormous datasets

Hints to guide your next attempt

  • Supervised = labeled data, Unsupervised = find patterns, RL = reward signal

Source tags: ml / supervised / unsupervised / reinforcement-learning / fundamentals

QUESTION 2 / medium / conceptual

What is the bias-variance tradeoff? How does it relate to overfitting and underfitting?

Sample answer

Bias: error from incorrect assumptions in the model. A high-bias model underfits — it's too simple to capture the underlying pattern. Example: fitting a linear model to data that has a quadratic relationship. Variance: error from sensitivity to fluctuations in training data. A high-variance model overfits — it memorizes training data including noise. Example: deep decision tree that perfectly fits training data but fails on new data. Total Error = Bias^2 + Variance + Irreducible Noise Bias-Variance Tradeoff: - Simple models (high bias, low variance): consistent but systematically wrong. Underfits. - Complex models (low bias, high variance): accurate on training, poor on test. Overfits. Diagnosing: - High bias (underfitting): training error AND test error are both high. Model is too simple. Fix: increase model complexity, add features, reduce regularization. - High variance (overfitting): training error is low but test error is much higher. Fix: get more training data, reduce model complexity, add regularization (L1/L2), dropout, early stopping. Regularization techniques that reduce variance: - L2 (Ridge): penalize large weights → smoother model - L1 (Lasso): drives some weights to zero → feature selection - Dropout (neural networks): randomly disable neurons during training → ensemble-like effect - Early stopping: stop training when validation error starts increasing

Hints to guide your next attempt

  • Bias = systematic error, Variance = sensitivity to training data noise
  • Total error = bias^2 + variance + irreducible noise

Source tags: ml / bias-variance / overfitting / underfitting / regularization

QUESTION 3 / medium / conceptual

What is gradient descent? Explain SGD, Mini-batch, and Adam optimizer.

Sample answer

Gradient Descent: optimization algorithm that iteratively adjusts model parameters to minimize the loss function by moving in the direction of the negative gradient. theta = theta - learning_rate * gradient(Loss) Variants: Batch Gradient Descent: compute gradient over entire dataset. - Pros: accurate gradient estimate, stable convergence - Cons: very slow for large datasets, can't use new data online - Use when: small datasets Stochastic Gradient Descent (SGD): compute gradient on ONE random sample at a time. - Pros: very fast updates, can escape local minima (noisy updates) - Cons: high variance, oscillates, needs learning rate decay - Use when: online learning, large datasets Mini-batch SGD: compute gradient on a small batch (32, 64, 128 samples). The standard in practice. - Pros: balanced speed and stability, vectorized operations on GPU, lower variance than SGD - Most deep learning uses mini-batch SGD or Adam Adam (Adaptive Moment Estimation): maintains adaptive learning rates per parameter. - Tracks first moment (mean of gradients) and second moment (mean of squared gradients) - Parameters with larger gradients get smaller learning rate updates (prevents oscillation) - Works well with little tuning — the default optimizer for most deep learning - Variants: AdamW (Adam + weight decay, better for transformers), RAdam, Lookahead Learning rate schedules: reduce learning rate over time. Step decay, cosine annealing, warmup + decay (for transformers). Vanishing/exploding gradients: deep networks struggle because gradients shrink or explode through many layers. Solutions: batch normalization, skip connections (ResNet), gradient clipping.

Hints to guide your next attempt

  • Gradient descent = iteratively move in direction that decreases loss
  • Adam = adaptive learning rates per parameter

Source tags: ml / gradient-descent / sgd / adam / optimization

QUESTION 4 / medium / conceptual

What are precision, recall, F1 score, and AUC-ROC? When would you optimize each?

Sample answer

Confusion matrix terms: TP = True Positive, TN = True Negative, FP = False Positive, FN = False Negative Precision = TP / (TP + FP): of all predicted positives, how many are actually positive? "When I predict spam, how often am I right?" Optimize when: False Positives are costly (spam filter — don't want to block legitimate email) Recall (Sensitivity) = TP / (TP + FN): of all actual positives, how many did I correctly identify? "Of all actual spam emails, how many did I catch?" Optimize when: False Negatives are costly (cancer detection — don't want to miss a case) F1 Score = 2 * (Precision * Recall) / (Precision + Recall): harmonic mean. Balances precision and recall. Use when: class imbalance and you need a single metric, both FP and FN matter equally. F-beta Score: weight recall more (beta > 1) or precision more (beta < 1). AUC-ROC (Area Under ROC Curve): - ROC curve: plots True Positive Rate (Recall) vs False Positive Rate at different classification thresholds - AUC = area under that curve. 0.5 = random classifier, 1.0 = perfect - Threshold-independent metric — evaluates model across all possible thresholds - Use when: comparing models, class imbalance, when you'll tune threshold later AUC-PR (Precision-Recall Curve): better for highly imbalanced datasets (rare positive class — fraud detection where fraud is 0.1%). Accuracy = (TP+TN)/(TP+TN+FP+FN): misleading for imbalanced classes (99% non-fraud → always predicting "not fraud" gives 99% accuracy). Practice: cancer screening → maximize recall (F2 score). Email spam filter → maximize precision. General classifier comparison → use AUC-ROC.

Hints to guide your next attempt

  • Precision = correctness of positives, Recall = completeness of positives
  • AUC-ROC = performance across all thresholds

Source tags: ml / precision / recall / f1 / auc-roc / evaluation

QUESTION 5 / medium / conceptual

What is cross-validation? Explain k-fold and stratified k-fold.

Sample answer

Cross-validation: technique to evaluate model generalization by training and testing on multiple different splits of the data. Why: a single train/test split can be "lucky" or "unlucky". Cross-validation gives a more reliable estimate of generalization performance. K-Fold Cross-Validation: 1. Split data into k equal folds (typically k=5 or k=10) 2. Train on k-1 folds, evaluate on 1 fold 3. Repeat k times, each fold serves as the test set once 4. Average the k evaluation scores from sklearn.model_selection import cross_val_score scores = cross_val_score(model, X, y, cv=5, scoring='f1') print(f"Mean F1: {scores.mean():.3f} +/- {scores.std():.3f}") Stratified K-Fold: each fold preserves the same class proportion as the full dataset. Use when: class imbalance — without stratification, one fold might have very few minority class examples. from sklearn.model_selection import StratifiedKFold Leave-One-Out (LOO): k = n (number of samples). Each sample is the test set once. Pros: maximum training data, nearly unbiased estimate Cons: computationally expensive, high variance estimate Time Series Cross-Validation (TimeSeriesSplit): for temporal data, always train on past, test on future. Never use random shuffling for time series — data leakage (using future data to predict past). Nested Cross-Validation: outer loop for model evaluation, inner loop for hyperparameter tuning. Gives an unbiased estimate of the final model's performance including the tuning process. Cross-validation vs simple train/val/test split: Use cross-validation when: small dataset, need reliable performance estimate Use simple split when: large dataset (CV is expensive), deep learning (train time too long)

Hints to guide your next attempt

  • Cross-validation prevents overfitting to a single train/test split
  • Stratified preserves class proportions in each fold

Source tags: ml / cross-validation / k-fold / model-evaluation / overfitting

QUESTION 6 / medium / conceptual

What is a decision tree? How does random forest improve on it?

Sample answer

Decision Tree: tree-based model where each internal node splits the data based on a feature threshold, each leaf predicts a class (classification) or value (regression). Splitting criterion: - Classification: Gini impurity or Information Gain (entropy) - Regression: mean squared error reduction Gini Impurity = 1 - sum(pi^2): measures how often a randomly chosen element would be incorrectly classified. Split that maximizes the reduction in Gini impurity. Pros: interpretable, handles mixed feature types, no scaling needed, works with missing values. Cons: prone to overfitting (especially deep trees), high variance — small changes in data → very different tree. Random Forest: ensemble of many decision trees, each trained with randomness to reduce correlation between trees. Two sources of randomness: 1. Bootstrap sampling (bagging): each tree trained on a random subset (with replacement) of training data (~63% unique samples) 2. Feature subsampling: at each split, consider only sqrt(n_features) random features instead of all Final prediction: majority vote (classification) or average (regression) of all trees. Why it reduces variance without increasing bias: - Individual trees have low bias, high variance - Averaging uncorrelated predictions reduces variance significantly - Random feature selection ensures trees are different even on similar data Hyperparameters: n_estimators (more = better, diminishing returns), max_depth, min_samples_split, max_features. Random Forest vs Gradient Boosting (XGBoost): - Random Forest: parallel training (fast), robust, good default performance, handles noise well - Gradient Boosting: sequential training, higher accuracy, more hyperparameter tuning needed, better for tabular competitions

Hints to guide your next attempt

  • Decision tree splits on features to maximize information gain
  • Random forest = ensemble of trees with randomness reducing variance

Source tags: ml / decision-tree / random-forest / ensemble / classification

QUESTION 7 / medium / conceptual

What is XGBoost? Why does it dominate tabular data competitions?

Sample answer

XGBoost (Extreme Gradient Boosting): gradient boosting framework that builds trees sequentially, where each tree corrects the errors of previous trees. Gradient Boosting idea: 1. Initialize prediction (e.g., mean of target) 2. Compute residuals (errors) 3. Fit a tree to predict residuals 4. Add tree predictions to ensemble (scaled by learning rate) 5. Repeat for n_estimators rounds Each new tree focuses on the mistakes of the current ensemble. Why XGBoost dominates tabular data: Regularization: L1 (alpha) and L2 (lambda) on leaf weights — prevents overfitting (unlike basic gradient boosting). Second-order approximation: uses both gradient AND Hessian (second derivative) of loss. Faster convergence, better splits. Tree pruning: builds trees depth-first then prunes (max_delta_step), more principled than level-wise. Missing value handling: learns the optimal direction for missing values during training. Speed: - Column subsampling (like Random Forest): reduces overfitting + faster - Parallel tree construction: splits computed in parallel (not sequential) - Cache-aware access: hardware-level optimization - Out-of-core computation: handles datasets larger than RAM Regularization prevents overfitting → can use many trees without degrading → captures complex patterns. vs LightGBM: LightGBM trains faster (leaf-wise tree growth, histogram-based algorithm) at similar accuracy. Often preferred for very large datasets. vs CatBoost: better for categorical features (native handling without encoding), less hyperparameter tuning. Usage (Python): import xgboost as xgb model = xgb.XGBClassifier(n_estimators=500, learning_rate=0.05, max_depth=6, subsample=0.8, colsample_bytree=0.8) model.fit(X_train, y_train, eval_set=[(X_val, y_val)], early_stopping_rounds=50)

Hints to guide your next attempt

  • XGBoost = gradient boosting with regularization, speed optimizations, and missing value handling

Source tags: ml / xgboost / gradient-boosting / tabular / ensemble

QUESTION 8 / medium / conceptual

What is Principal Component Analysis (PCA)? When would you use it?

Sample answer

PCA (Principal Component Analysis): linear dimensionality reduction technique. Finds orthogonal directions (principal components) in the feature space that capture the most variance in the data. Algorithm: 1. Standardize features (zero mean, unit variance) 2. Compute covariance matrix 3. Compute eigenvectors and eigenvalues of covariance matrix 4. Sort eigenvectors by eigenvalue (descending) — higher eigenvalue = more variance explained 5. Project data onto top k eigenvectors (principal components) Explained variance ratio: how much of total variance each component captures. Choose k to explain 90-95% of variance. from sklearn.decomposition import PCA pca = PCA(n_components=0.95) # keep 95% of variance X_reduced = pca.fit_transform(X_scaled) print(pca.explained_variance_ratio_) Use cases: - Curse of dimensionality: high-dimensional data → distances become meaningless. PCA reduces dimensions. - Visualization: reduce to 2-3 components for plotting - Remove noise: later components often capture noise — dropping them improves model - Speed up training: fewer features → faster model - Multicollinearity: correlated features → PCA creates uncorrelated components Limitations: - Linear only: doesn't capture non-linear relationships (use t-SNE, UMAP for non-linear) - Interpretability: components are mixtures of original features — hard to explain - Information loss: even at 95% variance, some information is discarded - Doesn't consider labels: PCA is unsupervised — use LDA (Linear Discriminant Analysis) for supervised dimensionality reduction When NOT to use PCA: when features are already meaningful and few, when you need interpretability, when relationships are non-linear.

Hints to guide your next attempt

  • PCA = project data onto directions of maximum variance
  • Reduces dimensionality while preserving most information

Source tags: ml / pca / dimensionality-reduction / unsupervised / feature-engineering

QUESTION 9 / medium / conceptual

What is K-means clustering? What are its limitations and when should you use DBSCAN instead?

Sample answer

K-means Clustering: partition n points into k clusters by minimizing within-cluster sum of squared distances (inertia). Algorithm: 1. Initialize k centroids (randomly or K-means++ for better init) 2. Assign each point to nearest centroid 3. Update centroids as mean of assigned points 4. Repeat until convergence (centroids stop moving) from sklearn.cluster import KMeans kmeans = KMeans(n_clusters=5, init='k-means++', n_init=10) labels = kmeans.fit_predict(X) Choosing k: elbow method (plot inertia vs k, pick "elbow"), silhouette score. K-means limitations: - Must specify k upfront - Assumes spherical, similarly-sized clusters - Sensitive to outliers (centroid pulled by outliers) - Only finds convex clusters (fails for crescent shapes, rings) - Sensitive to feature scale — always scale before K-means DBSCAN (Density-Based Spatial Clustering of Applications with Noise): - Groups points that are closely packed together, marks outliers as noise - Parameters: eps (radius), min_samples (minimum neighbors to form a core point) - Finds clusters of arbitrary shape - Automatically determines number of clusters - Robust to outliers (they become noise points, not assigned to any cluster) DBSCAN limitations: - Struggles with varying density clusters - eps and min_samples sensitive — hard to tune - High-dimensional data: distances become less meaningful Use K-means when: clusters roughly spherical, know k, large dataset, need fast algorithm. Use DBSCAN when: don't know k, arbitrary cluster shapes, have noise/outliers, geographic data. Other options: Hierarchical clustering (dendrogram, no need to specify k), Gaussian Mixture Models (soft assignments, probabilistic).

Hints to guide your next attempt

  • K-means assumes spherical clusters of similar size
  • DBSCAN finds arbitrary shapes and handles noise

Source tags: ml / k-means / clustering / dbscan / unsupervised

QUESTION 10 / medium / conceptual

What is a neural network? Explain forward pass, backpropagation, and activation functions.

Sample answer

Neural Network: computation graph of layers of neurons. Each neuron computes a weighted sum of inputs plus bias, then applies an activation function. Architecture: - Input layer: raw features - Hidden layers: learned representations - Output layer: predictions Forward Pass: compute predictions from input to output. z = W * x + b (linear transformation) a = activation(z) (non-linearity) Backpropagation: compute gradient of loss with respect to each parameter using chain rule. dL/dW = dL/da * da/dz * dz/dW Gradients flow backward from output to input. Update each weight: W = W - lr * dL/dW. Activation Functions: ReLU (Rectified Linear Unit): max(0, x). Most common in hidden layers. Pros: computationally fast, doesn't saturate for positive inputs, sparse activation. Cons: "dying ReLU" — neurons stuck at 0 if inputs always negative. Leaky ReLU: max(0.01x, x). Fixes dying ReLU. Sigmoid: 1/(1+e^-x). Output in (0,1). Used in binary classification output layer. Cons: vanishing gradient for extreme inputs, not zero-centered. Tanh: (e^x - e^-x)/(e^x + e^-x). Output in (-1,1). Zero-centered. Cons: vanishing gradient. Softmax: normalizes outputs to sum to 1 — probability distribution. Used in multi-class classification output layer. GELU, SiLU: smooth approximations of ReLU. Used in transformers (BERT, GPT use GELU). Vanishing gradient: sigmoid/tanh saturate → gradient ≈ 0 in deep networks → early layers don't learn. Fix: ReLU, batch normalization, residual connections.

Hints to guide your next attempt

  • Forward pass = compute predictions, Backprop = compute gradients via chain rule

Source tags: ml / neural-network / backpropagation / activation-functions / deep-learning

QUESTION 11 / medium / conceptual

What is a Convolutional Neural Network (CNN)? How does it process images?

Sample answer

CNN: neural network with convolutional layers that learn spatial feature detectors (filters/kernels). Key layers: Convolutional Layer: applies learnable filters to the input. - Filter: small matrix (e.g., 3x3) that slides over the image - Each filter detects a specific pattern: edges, colors, textures, later layers detect higher-level features (eyes, wheels) - Output: feature map (one per filter) - Parameters: filter_height * filter_width * in_channels * num_filters Padding: add zeros around input to control output spatial dimensions. Stride: how far the filter moves each step. Larger stride = smaller output. Pooling Layer: downsamples feature maps, reducing spatial dimensions. - Max pooling: takes max value in each region → retains most prominent feature - Average pooling: takes average → smoother - Reduces computation, provides some translation invariance Flattening + Fully Connected: after conv/pooling layers, flatten and apply FC layers for classification. Typical CNN flow: Input image → [Conv + ReLU + MaxPool] x N → Flatten → FC layers → Softmax output Why CNNs work for images: - Parameter sharing: same filter applied everywhere → far fewer parameters than FC on raw pixels - Translation invariance: learns to detect a feature regardless of where it appears - Hierarchical features: early layers = edges/textures, middle = shapes, deep = objects Modern architectures: - VGG: very deep, 3x3 convolutions throughout - ResNet: skip/residual connections — enables training 50-200+ layer networks - EfficientNet: compound scaling — scale depth, width, resolution together - Vision Transformer (ViT): apply transformer attention to image patches

Hints to guide your next attempt

  • CNN = learnable filters that slide over the image detecting features
  • Translation invariant — finds pattern regardless of position

Source tags: ml / cnn / convolutional / image-classification / deep-learning

QUESTION 12 / hard / conceptual

What is an RNN? Why do LSTMs and GRUs solve the vanishing gradient problem?

Sample answer

RNN (Recurrent Neural Network): processes sequences by maintaining a hidden state that carries information from previous timesteps. h_t = tanh(W_hh * h_{t-1} + W_xh * x_t + b) output_t = W_hy * h_t Problem: vanishing gradients over long sequences. Gradient signal from timestep t=100 must propagate back through 100 matrix multiplications — if weights < 1, gradients shrink exponentially → early timesteps don't learn. LSTM (Long Short-Term Memory): two state vectors: hidden state (h) + cell state (C). Gates control information flow. Gates: - Forget gate: f_t = sigmoid(W_f * [h_{t-1}, x_t] + b_f). How much of previous cell state to keep. - Input gate: i_t = sigmoid(W_i * [h_{t-1}, x_t] + b_i). Which new info to add. - Candidate cell: g_t = tanh(W_g * [h_{t-1}, x_t] + b_g). New candidate values. - Cell update: C_t = f_t * C_{t-1} + i_t * g_t. Additive update (gradient highway!). - Output gate: h_t = o_t * tanh(C_t). Why LSTM solves vanishing gradient: the cell state update is ADDITIVE, not multiplicative. Gradient can flow back through the cell state pathway without repeated multiplication → learns long-range dependencies. GRU (Gated Recurrent Unit): simplified LSTM with reset and update gates (no separate cell state). - Fewer parameters than LSTM, often similar performance - Reset gate: how much of previous hidden state to forget - Update gate: balance between previous and new hidden state When to use: - LSTM/GRU: time series, speech, language modeling (pre-transformer era) - Transformers have largely replaced RNNs for NLP (parallel training, better long-range attention) - RNNs still useful for: streaming/online inference, very long sequences with memory constraints, small models

Hints to guide your next attempt

  • RNN = hidden state carries information forward
  • LSTM gates control what to remember/forget

Source tags: ml / rnn / lstm / gru / sequence / vanishing-gradient

Check understanding, not recognition

  • Can you explain the answer without reading it?
  • Can you show when the approach fails or needs a different assumption?
  • Can you connect the concept to a real project, task, or decision you have made?
  • Can you distinguish a simplified interview example from a production requirement?

A LITTLE MORE CLARITY

Frequently asked questions

How many ML & AI questions are shown here?

This public preview shows 12 questions with the supplied answers from the app's 52-question ML & AI bank. Full interactive practice requires signing in.

Should I memorize the sample answers?

Use them as reference explanations, not a script or a guarantee of technical correctness for every environment. Explain your assumptions, validate code against your language and version, and discuss trade-offs. Behavioral answers should be built from your own experience.

What do hints and solution steps mean?

Hints identify ideas to consider before reading the answer. When the source question includes steps, they are shown as a separate walkthrough. Example capacities and design targets in a solution are assumptions for that exercise, not real system statistics.

BUILD YOUR NEXT OPPORTUNITY

Practice ML & AI actively.

Sign in to use the full topic bank and interactive practice workspace.

Open Topic Practice