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
  20. Can you handle class imbalance without changing the data? What is threshold tuning?

    Yes. Most classifiers output a probability, and the default cut-off of 0.5 is arbitrary. Lowering it for a rare class raises recall at the cost of precision. Pick the threshold from the precision-recall curve using business costs.

    IntermediateQuick answer1 min read
  21. Fit a line to the points (1,2), (2,4), (3,5) and compute R squared.

    Slope = 1.5, intercept = 0.667, so y = 0.667 + 1.5x. R squared = 1 - 0.1667/4.6667, about 0.964: the line explains about 96% of the variation in y.

    IntermediateQuick answer1 min read
  22. How can scaling or imputing before the train-test split cause data leakage?

    If you compute the mean or maximum on the full dataset, information from test rows leaks into training. Always fit scalers and imputers on the training set only, then apply them to the test set. A scikit-learn Pipeline does this automatically.

    IntermediateQuick answer1 min read
  23. How do class weights, oversampling and undersampling differ for imbalanced data?

    Class weights make mistakes on the rare class cost more during training. Oversampling duplicates or synthesises minority rows; undersampling removes majority rows. Weights change no data, oversampling can overfit, undersampling can throw away useful rows.

    IntermediateQuick answer1 min read
  24. How do you interpret a coefficient in logistic regression?

    Each coefficient is the change in log-odds for a one-unit rise in that feature. Exponentiate it to get an odds ratio: a coefficient of 0.693 multiplies the odds by about 2.

    IntermediateQuick answer1 min read
  25. How do you write a generic arrow function in a .tsx file?

    In .tsx, <T>(x: T) => x is parsed as a JSX tag and fails. Write <T,>(x: T) => x with a trailing comma, or <T extends unknown>, or use a regular function declaration instead.

    IntermediateQuick answer1 min read
  26. How does a decision tree predict a number in regression?

    It splits the data to reduce squared error and each leaf predicts the average target of the rows in it. The output is therefore piecewise constant, a staircase, not a smooth line.

    IntermediateQuick answer1 min read
  27. How does Naive Bayes classify an email as spam? Work through a small example.

    It uses Bayes' theorem: posterior is proportional to prior times the product of each word's likelihood given the class. Compute that score for spam and for not-spam, then normalise. The class with the higher posterior wins.

    IntermediateQuick answer1 min read
  28. How does SMOTE work, and what are its limitations?

    SMOTE creates synthetic minority samples by picking a minority point, choosing one of its nearest minority neighbours, and placing a new point at a random spot on the line between them. It reduces duplication but can create noisy points near class borders.

    IntermediateQuick answer1 min read
  29. How would you encode a categorical feature with thousands of values, like a pincode?

    One-hot would create thousands of sparse columns. Better options are target (mean) encoding computed out-of-fold, frequency encoding, grouping rare values into 'other', hashing, or learned embeddings.

    IntermediateQuick answer1 min read
  30. What are entropy and information gain in a decision tree?

    Entropy measures how mixed a group is: 0 when pure, 1 for a 50-50 split of two classes. Information gain is the entropy before a split minus the weighted entropy after. The tree picks the split with the highest gain.

    IntermediateQuick answer1 min read