Interview Q&A / Machine Learning

INTERVIEW Q&A · 55 QUESTIONS

Machine Learning interview questions

Supervised vs unsupervised learning, overfitting, metrics and the classic models, explained simply.

  1. A fraud model shows 99% accuracy. Why might it still be useless?

    If only 1% of transactions are fraud, a model that always says 'not fraud' scores 99% accuracy while catching zero fraud. On imbalanced data, look at recall, precision, F1 or PR-AUC instead.

    BeginnerQuick answer1 min read
  2. Give three real-world examples each of supervised and unsupervised learning.

    Supervised: spam filtering, house price prediction, loan default prediction (all have known past answers). Unsupervised: customer segmentation, anomaly detection in UPI transactions, topic discovery in news articles (no answers given).

    BeginnerQuick answer1 min read
  3. How do you choose k in KNN, and what happens when k is very small or very large?

    Small k (like 1) follows noise and overfits; very large k smooths too much and underfits, at the extreme predicting the majority class for everything. Choose k by cross-validation, and use an odd k for two-class problems to avoid ties.

    BeginnerQuick answer1 min read
  4. How do you handle missing values in a dataset?

    First understand why values are missing. Then drop rows or columns if few are affected, or impute with mean, median, mode, a constant, or a model-based estimate. Adding a 'was missing' flag often helps. Fit the imputer on training data only.

    BeginnerQuick answer1 min read
  5. How do you restrict a generic type parameter using extends?

    Write T extends Something to say the caller may pass any type, as long as it has at least the shape of Something. Inside the function you can then safely use those members, such as .length or .id.

    BeginnerQuick answer1 min read
  6. How do you stop a decision tree from overfitting?

    Limit its growth (max_depth, min_samples_leaf, min_samples_split, max_leaf_nodes) or prune it afterwards with cost-complexity pruning (ccp_alpha). Choose the settings by cross-validation. Or use an ensemble such as a random forest.

    BeginnerQuick answer1 min read
  7. How do you write a generic class in TypeScript, for example a Stack?

    Put the type parameter after the class name, like class Stack<T>, and use T in fields and method signatures. Each instance fixes T when created, so new Stack<number>() only accepts and returns numbers.

    BeginnerQuick answer1 min read
  8. How does logistic regression turn a score into a probability?

    It computes a linear score z = w.x + b and passes it through the sigmoid 1/(1+e^-z), which squashes any number into 0 to 1. A threshold, usually 0.5, then converts the probability into a class.

    BeginnerQuick answer1 min read
  9. How does reinforcement learning work? Explain agent, environment, reward and policy.

    An agent takes actions in an environment, receives a reward and a new state, and learns a policy (a rule for choosing actions) that maximises total reward over time. It learns by trial and error, not from labelled examples.

    BeginnerQuick answer1 min read
  10. How does the K-Nearest Neighbours algorithm make a prediction?

    KNN stores all training data. To predict, it finds the k training points closest to the new point and takes a majority vote (classification) or an average (regression) of their targets.

    BeginnerQuick answer1 min read
  11. How would you model an API response with a generic type?

    Define one generic interface such as ApiResponse<T> with a data: T field plus shared fields like status and message. Each endpoint then reuses it with its own data type, for example ApiResponse<User[]>.

    BeginnerQuick answer1 min read
  12. What is a support vector machine, and what are support vectors and margin?

    An SVM finds the separating line (hyperplane) with the widest margin between two classes. Support vectors are the training points closest to that boundary; only they decide where it sits.

    BeginnerQuick answer1 min read
  13. What is the difference between classification and regression?

    Classification predicts a category (spam or not spam, pass or fail). Regression predicts a continuous number (a house price, tomorrow's temperature). The choice depends on the type of the target column, not on the algorithm.

    BeginnerQuick answer1 min read
  14. What is the difference between Euclidean and Manhattan distance, and which should KNN use?

    Euclidean is the straight-line distance, sqrt of the sum of squared differences. Manhattan is the sum of absolute differences, like walking along city blocks. Euclidean is the default; Manhattan can be better in high dimensions or with many outliers.

    BeginnerQuick answer1 min read
  15. What is the difference between label encoding and one-hot encoding?

    Label encoding maps each category to an integer, which falsely implies an order. One-hot encoding creates one 0/1 column per category with no implied order. Use one-hot for unordered categories in linear models; integer codes are fine for ordered ones or tree models.

    BeginnerQuick answer1 min read
  16. What is the difference between standardisation and min-max scaling?

    Standardisation subtracts the mean and divides by the standard deviation, giving mean 0 and spread 1. Min-max scaling squeezes values into a fixed range, usually 0 to 1. Min-max is more affected by outliers.

    BeginnerQuick answer1 min read
  17. When should you use median instead of mean to fill missing values?

    Use the median when the column has outliers or is skewed, because the mean gets dragged by extreme values. Use the mean for roughly symmetric data without outliers.

    BeginnerQuick answer1 min read
  18. Why do we scale features, and which algorithms need it?

    Scaling puts features on comparable ranges. Distance-based and gradient-based models (KNN, SVM, k-means, PCA, neural networks, regularised regression) need it. Tree-based models do not.

    BeginnerQuick answer1 min read
  19. Why is Naive Bayes called 'naive', and why does it still work?

    It naively assumes that features are independent of each other once the class is known. That is rarely true, but classification only needs the correct class to have the highest score, so predictions are often good even when the probabilities are off.

    BeginnerQuick answer1 min read