Machine Learning Interview Questions: A Comprehensive Guide
Machine Learning (ML) concepts form the foundation of how models are built, trained, and evaluated. In interviews, questions are often asked around these core ideas, testing both theoretical knowledge and practical application. Machine Learning Engineers are the builders who transform data science experiments into production-grade AI systems that deliver real business value. They combine a deep understanding of algorithms and mathematics with software engineering excellence to create scalable, reliable ML solutions.
Understanding Machine Learning Fundamentals
What is Machine Learning?
Machine Learning (ML) is a branch of Artificial Intelligence (AI) that focuses on building algorithms capable of learning from data. Instead of being explicitly programmed with fixed rules, these algorithms identify patterns in data and use them to make predictions or decisions that improve with experience.
| Aspect | Artificial Intelligence (AI) | Machine Learning (ML) | Data Science |
|---|---|---|---|
| Definition | Broad field aiming to build systems that mimic human intelligence | Subset of AI that learns patterns from data for prediction or decision-making | Field focused on extracting insights and knowledge from data |
| Scope | Reasoning, problem-solving, planning, natural language, robotics | Algorithms for classification, regression, clustering, etc. | Data collection, cleaning, analysis, visualization, ML, and reporting |
| Techniques Used | Expert systems, NLP, robotics, ML, deep learning | Regression, decision trees, neural networks, clustering | Statistics, ML, data visualization, domain knowledge |
| Example | Chatbots, self-driving cars, expert systems | Spam detection, recommendation systems, fraud detection | Analyzing sales trends, customer segmentation, forecasting |
Supervised vs. Unsupervised Learning
Supervised learning algorithms infer a function from labeled training data, where the training data consists of a set of training examples. For example, knowing the height and weight to identify the gender of a person. Supervised learning requires training labeled data. For example, to do classification (a supervised learning task), you’ll need to first label the data you’ll use to train the model to classify data into your labeled groups.
Unsupervised learning algorithms, on the other hand, are used to find patterns on the set of data given. In this, we don’t have any dependent variable or label to predict.
Generative vs. Discriminative Models
A generative model learns categories of data, while a discriminative model simply learns the distinction between different categories of data. Generative models model the joint probability distribution between X and Y. Discriminative models directly learn a decision boundary by choosing a class that maximizes the posterior probability distribution.
Read also: Comprehensive Interview Guide
Addressing Overfitting and Underfitting
Overfitting
Overfitting occurs when a model learns the true patterns in the training data and memorizes the noise or random fluctuations. This results in high accuracy on training data but poor performance on unseen/test data. Essentially, if you make the model more complex and add more variables, you’ll lose bias but gain some variance - in order to get the optimally reduced amount of error, you’ll have to tradeoff bias and variance.
Ways to Avoid Overfitting:
- Early Stopping: Stop training when validation accuracy stops improving, even if training accuracy is still increasing.
- Regularization: Apply techniques like L1 (Lasso) or L2 (Ridge) regularization, which add penalties to large weights to reduce model complexity.
- Cross-Validation: Use k-fold cross-validation to ensure the model generalizes well.
- Dropout (for Neural Networks): Randomly drop neurons during training to prevent over-reliance on specific nodes.
- Simpler Models: Avoid overly complex models when simpler ones can explain the data well.
Underfitting
Underfitting occurs when a model is too simple to capture the underlying patterns in the data. This leads to poor accuracy on both training and test data.
Ways to Avoid Underfitting:
- Use a More Complex Model: Choose models with higher complexity to learn patterns like decision trees, neural networks, etc.
- Add Relevant Features: Include meaningful features that better represent the data.
- Reduce Regularization: Too much regularization can restrict the model’s ability to learn.
- Train Longer: Allow the model more epochs or iterations to properly learn patterns.
Regularization Techniques
Regularization is a technique used to reduce model complexity and prevent overfitting. It works by adding a penalty term to the loss function to discourage the model from assigning too much importance (large weights) to specific features. This helps the model generalize better on unseen data.
Ways to Apply Regularization:
L1 Regularization (Lasso): Adds the absolute value of weights as a penalty, which can shrink some weights to zero and perform feature selection. Lasso regularization tends to spread error among all the terms, while L1 is more binary/sparse, with many variables either being assigned a 1 or 0 in weighting.
- Formula: (\text{Lasso Loss} = \text{MSE} + \lambda \sum{i=1}^{n} |wi|)
L2 Regularization (Ridge): Adds the squared value of weights as a penalty, which reduces large weights but doesn’t eliminate them.
Read also: College Recruitment Coding Prep
- Formula: (\text{Ridge Loss} = \text{MSE} + \lambda \sum{i=1}^{n} wi^2)
Elastic Net: Combines both L1 and L2 penalties to balance feature selection and weight reduction. It is especially useful when features are correlated, as it avoids Lasso’s limitation of picking only one feature from a group.
Dropout (for Neural Networks): Randomly drops neurons during training to avoid over-reliance on specific nodes.
Key Differences Between Lasso (L1) and Ridge (L2):
- Lasso (L1): Can set weights to zero → feature selection. Use it when we have many irrelevant features.
- Ridge (L2): Reduces weights but keeps all features → no feature elimination. Use when all features are useful but want to avoid overfitting.
Elastic Net combines both L1 (Lasso) and L2 (Ridge) penalties, balancing feature selection and weight reduction. It is especially useful when features are correlated, as it avoids Lasso’s limitation of picking only one feature from a group.
Loss Functions and Cost Functions
When calculating loss, we consider only a single data point, then we use the term loss function. Whereas, when calculating the sum of error for multiple data then we use the cost function. In other words, the loss function is to capture the difference between the actual and predicted values for a single record, whereas cost functions aggregate the difference for the entire training dataset.
Loss functions measure the error between the model’s predicted output and the actual target value. They guide the optimization process during training.
Read also: A Guide to Interview Magazine Internships
Common Loss Functions:
Mean Squared Error (MSE): Used in regression problems. It penalizes larger errors more heavily by squaring them.
- Formula: (MSE = \frac{1}{n}\sum{i=1}^{n}(yi - \hat{y}_i)^2)
Mean Absolute Error (MAE): Used in regression as it takes absolute differences between predicted and actual values. It is less sensitive to outliers than MSE.
- Formula: (MAE = \frac{1}{n}\sum{i=1}^{n}\lvert yi - \hat{y}_i \rvert)
Huber Loss: Combines MSE and MAE, making it less sensitive to outliers than MSE.
Cross-Entropy Loss (Log Loss): Used in classification problems. It measures the difference between predicted probability distribution and actual labels.
- Formula: (CE = -\frac{1}{n} \sum{i=1}^{n}\big[yi \log(\hat{y}i) + (1-yi)\log(1-\hat{y}_i)\big])
Hinge Loss: Used for classification with SVMs. It encourages maximum margin between classes.
KL Divergence: Measures how one probability distribution differs from another; hence, used in probabilistic models.
Exponential Loss: Used in boosting methods like AdaBoost; penalizes misclassified points more strongly.
R-squared (R²): Used in regression and measures how well the model explains variance in the target variable.
- Formula: (R^2 = 1 - \frac{\sum{i=1}^{n} (yi - \hat{y}i)^2}{\sum{i=1}^{n} (y_i - \bar{y})^2})
Model Evaluation Techniques
Model evaluation techniques are used to assess how well a machine learning model performs on unseen data. Choosing the right technique depends on the type of problem (classification, regression, etc.) and the type of dataset we have.
Common Evaluation Techniques:
- Train-Test Split: Divide data into training and testing sets (e.g., 70:30 or 80:20) to evaluate model performance on unseen data. Here, 70% data will be used for training and 30% will be used to test the accuracy of the model.
- Cross-Validation: Split data into k folds, train on k-1 folds, validate on the remaining fold, and average the results to reduce bias.
- Confusion Matrix (for Classification): Counts True Positives, True Negatives, False Positives, and False Negatives.
- Accuracy: Proportion of correct predictions over total predictions.
- Precision: Correct positive predictions divided by total predicted positives.
- Recall (Sensitivity): Correct positive predictions divided by total actual positives.
- F1-Score: Harmonic mean of precision and recall. It balances precision and recall.
- ROC Curve & AUC: Measures model’s ability to distinguish between classes. The AUC is the area under the ROC curve.
- Loss Functions (for Regression/Classification): Quantifies prediction error to optimize the model. It can include Mean Absolute Error (MAE), Mean Squared Error (MSE), Root Mean Squared Error (RMSE), etc.
Confusion Matrix
A confusion matrix is a table used to evaluate the performance of a classification model. It compares the predicted labels with the actual labels, telling how well the model is performing and what types of errors it makes.
| Predicted Positive | Predicted Negative | |
|---|---|---|
| Actual Positive | True Positives (TP) | False Negatives (FN) |
| Actual Negative | False Positives (FP) | True Negatives (TN) |
- True Positives (TP): Correctly predicted positive cases.
- True Negatives (TN): Correctly predicted negative cases.
- False Positives (FP): Negative cases incorrectly predicted as positive. (Type I error)
- False Negatives (FN): Positive cases incorrectly predicted as negative. (Type II error)
It is used in metrics like Accuracy, Precision, Recall, and F1-Score.
Precision vs. Recall
Precision: It is the ratio between the true positives (TP) and all the positive examples (TP+FP) predicted by the model. In other words, precision measures how many of the predicted positive examples are actually true positives.
- Formula: (\text{Precision} = \frac{TP}{TP + FP})
Recall: Recall measures how many of the actual positive examples are correctly identified by the model. It is a measure of the model's ability to avoid false negatives and identify all positive examples correctly. Also known as the true positive rate.
- Formula: (\text{Recall}=\frac{TP}{TP + FN})
Key Difference:
- Precision is about being exact (avoiding false positives).
- Recall is about being comprehensive (avoiding false negatives).
F1-Score
The F1 score is a measure of a model’s performance. It is a weighted average of the precision and recall of a model, with results tending to 1 being the best, and those tending to 0 being the worst.
- Formula: (F1 = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision + Recall}})
Used when both precision and recall matter.
ROC Curve and AUC
ROC Curve (Receiver Operating Characteristic): The ROC curve is a graphical plot that shows the trade-off between True Positive Rate (TPR / Recall) and False Positive Rate (FPR) at different threshold values.
- (TPR \text{ (Recall)} = \frac{TP}{TP + FN})
- (FPR = \frac{FP}{FP + TN})
AUC (Area Under the Curve): AUC is the area under the ROC curve. It represents the probability that a randomly chosen positive instance is ranked higher than a randomly chosen negative instance.
- AUC = 1 → Perfect classifier
- AUC = 0.5 → Random guessing
- AUC < 0.5 → Worse than random
ROC shows performance across thresholds. AUC summarizes overall model performance into a single number.
Is Accuracy Always a Good Metric?
No, accuracy can be misleading, especially with imbalanced datasets. In such cases:
- Precision and Recall provide better insight into model performance.
- F1-score combines precision and recall as their harmonic mean, giving a balanced measure of model effectiveness, especially when the classes are imbalanced.
An imbalanced dataset is when you have, for example, a classification test and 90% of the data is in one class. That leads to problems: an accuracy of 90% can be skewed if you have no predictive power on the other category of data!
Cross-Validation Techniques
Cross-validation is a model evaluation technique used to test how well a machine learning model generalizes to unseen data. Instead of training and testing on a single split, the dataset is divided into multiple subsets (called folds) and the model is trained and tested multiple times on different folds.
How It Works:
- Split the dataset into k folds (e.g., 5 or 10).
- Train the model on (k-1) folds and test it on the remaining fold.
- Repeat this process k times so that every fold is used for testing once.
- Take the average of all results as the final performance score.
Types of Cross-Validation:
k-Fold Cross-Validation: The dataset is divided into k equal folds. The model is trained on (k-1) folds and tested on the remaining fold. This process is repeated k times, with each fold used once as the test set. The final score is the average of all k test results.
- Formula: (CV{error} = \frac{1}{k} \sum{i=1}^{k} error_i)
Stratified k-Fold: Similar to k-Fold but keeps class distribution balanced (useful in classification).
Leave-One-Out (LOO): A special case where k = number of samples and every single point acts as a test set once.
Hold-Out Method: The simplest technique where the dataset is split into two parts: a training set and a testing set (e.g., 70% train, 30% test). The model is trained on the training set and evaluated on the test set. It is fast but may lead to biased results depending on the split. Simple train/test split and is considered a basic form of validation.
Feature Engineering and Scaling
Feature Engineering
Feature engineering is the process of creating, transforming, or selecting relevant features from raw data to improve the performance of a machine learning model. Better features often lead to better model accuracy and generalization. It also reduces overfitting and make the model easier to interpret.
Key Steps in Feature Engineering:
- Feature Creation: Generate new features from existing data like extracting “year” or “month” from a date column.
- Feature Transformation: Apply scaling, normalization, or mathematical transformations (log, square root) to features.
- Feature Encoding: Convert categorical variables into numerical form like one-hot encoding, label encoding.
- Feature Selection: Identify and keep only the most relevant features using techniques like correlation analysis, mutual information, or model-based importance scores.
Feature Scaling
Standardization: A preprocessing step that rescales features so they have a mean = 0 and standard deviation = 1. Useful for algorithms sensitive to feature scales like SVM, KNN, Logistic Regression, etc.
- Formula: (x' = \frac{x - \mu}{\sigma})
Normalization: A preprocessing step that rescales feature values into a fixed range, usually [0, 1]. Useful when features have very different scales or units.
- Formula: (x' = \frac{x - x{min}}{x{max} - x_{min}})
| Aspect | Regularization | Standardization | Normalization |
|---|---|---|---|
| Purpose | Prevent overfitting | Rescale features (mean = 0, std = 1) | Rescale features to a range (e.g., [0,1]) |
| Works On | Model weights | Input features | Input features |
| Main Idea | Add penalty to loss function | Center and scale features | Shrink features into a fixed range |
| Example | L1, L2, Elastic Net | Z-score scaling | Min-Max scaling |
| When to Use | High variance/overfitting | Algorithms needing Gaussian-like distribution | Features with different ranges/units |
Common Machine Learning Algorithms
Random Forest
Random forest is a versatile machine learning method capable of performing both regression and classification tasks. Like bagging and boosting, random forest works by combining a set of other tree models. Random Forests create a series of decision trees, where each tree is trained using randomly chosen bootstrapped samples of data.
A random forest is an ensemble method that utilizes many decision trees and averages the decision from them. It reduces overfitting and correlation between the trees by two methods: 1) bagging (bootstrap aggregation), whereby some m < n (where n is the total number of) data points are arbitrarily sampled with replacement and used as the training set, 2) a random subset of the features are
Support Vector Machines (SVM)
A Support Vector Machine (SVM) is a very powerful and versatile supervised machine learning model, capable of performing linear or non-linear classification, regression, and even outlier detection.
In SVM, a data point is viewed as a p-dimensional vector (a list of p numbers), and we wanted to know whether we can separate such points with a (p-1)-dimensional hyperplane. This is called a linear classifier. There are many hyperplanes that classify the data. To choose the best hyperplane that represents the largest separation or margin between the two classes. If such a hyperplane exists, it is known as a maximum-margin hyperplane and the linear classifier it defines is known as a maximum margin classifier.
Suppose we have given some data points that each belong to one of two classes, and the goal is to separate two classes based on a set of examples.
Given data ((x1, y1), …, (xn, yn)), and different features ((x{i1}, …, x{ip})), and (y_i) is either 1 or -1. The equation of the hyperplane is the set of points satisfying:
[w \cdot x - b = 0]
Where (w) is the normal vector of the hyperplane. The parameter (\frac{b}{||w||}) determines the offset of the hyperplane from the original along the normal vector (w).
tags: #machine #learning #interview #questions

