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. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. What are the main assumptions of linear regression?

    Linearity between features and target, independent errors, constant error variance (homoscedasticity), roughly normal errors (needed mainly for confidence intervals), and no severe multicollinearity among features.

    IntermediateQuick answer1 min read
  13. What do the C and gamma parameters do in an RBF SVM?

    C controls how much misclassification is tolerated: high C fits training data tightly (risk overfitting), low C allows a wider, smoother margin. Gamma sets how far one point's influence reaches: high gamma gives wiggly, local boundaries, low gamma gives smooth ones.

    IntermediateQuick answer1 min read
  14. What is a default type parameter in a generic, and when is it useful?

    A default like <T = string> is used when the caller gives no type argument and TypeScript cannot infer one. It keeps simple usage short while still allowing a custom type when needed.

    IntermediateQuick answer1 min read
  15. What is bootstrap sampling and the out-of-bag score in a random forest?

    Each tree trains on a bootstrap sample: n rows drawn with replacement, containing about 63% of the unique rows. The remaining roughly 37% are out-of-bag for that tree and give a free validation estimate.

    IntermediateQuick answer1 min read
  16. What is Laplace smoothing in Naive Bayes, and why is it needed?

    If a word never appeared with a class in training, its probability is zero and wipes out the whole product. Laplace smoothing adds 1 to every word count so no probability is exactly zero.

    IntermediateQuick answer1 min read
  17. What is log loss (cross-entropy), and why does logistic regression not use mean squared error?

    Log loss is -ln(p) for the probability given to the true class. It punishes confident wrong predictions very heavily. With a sigmoid, squared error gives a non-convex loss and weak gradients; log loss is convex and gives clean gradients.

    IntermediateQuick answer1 min read
  18. What is multicollinearity, and how do you detect and fix it?

    Multicollinearity means two or more features are strongly correlated, so a linear model cannot tell their effects apart and coefficients become unstable. Detect it with a correlation matrix or VIF above about 5-10. Fix it by dropping or combining features, PCA, or ridge regression.

    IntermediateQuick answer1 min read
  19. What is self-supervised learning, and how is it different from unsupervised learning?

    Self-supervised learning creates its own labels from the data, such as hiding a word and predicting it. Unsupervised learning has no target at all and looks for structure like clusters. Large language models are pre-trained in a self-supervised way.

    IntermediateQuick answer1 min read
  20. What is semi-supervised learning, and when is it useful?

    Semi-supervised learning trains on a small labelled set plus a large unlabelled set. It helps when labels are expensive but raw data is cheap, for example medical scans or speech recordings.

    IntermediateQuick answer1 min read
  21. What is the difference between Gaussian, Multinomial and Bernoulli Naive Bayes?

    Gaussian NB assumes each feature is normally distributed within a class, so it suits continuous values. Multinomial NB models word counts. Bernoulli NB models binary present/absent features.

    IntermediateQuick answer1 min read
  22. What is the kernel trick in SVM?

    A kernel computes the dot product of two points as if they were mapped to a higher-dimensional space, without actually building that space. It lets a linear SVM draw curved boundaries cheaply. Common kernels: linear, polynomial, RBF.

    IntermediateQuick answer1 min read
  23. When does TypeScript infer generic type arguments, and when must you pass them yourself?

    TypeScript infers type arguments from the values you pass. You must write them explicitly when no argument carries the type, such as an empty array, a function with only a return type, or a call where you want a wider type than the inferred one.

    IntermediateQuick answer1 min read
  24. When should you use a generic instead of a union type for a function parameter?

    Use a generic when the output type must follow the input type, such as returning the same kind you received. Use a union when the function accepts several kinds and the result does not depend on which one it was.

    IntermediateQuick answer1 min read
  25. When would you pick an SVM over logistic regression, and the other way round?

    Pick logistic regression when you want calibrated probabilities, speed and interpretable coefficients. Pick an SVM (often with RBF kernel) when classes are not linearly separable and the dataset is small to medium. Both need scaled features.

    IntermediateQuick answer1 min read
  26. Which hyperparameters matter most in a random forest?

    n_estimators (more trees gives stable results with diminishing returns), max_features (how many features each split considers), max_depth, min_samples_leaf and min_samples_split. max_features and min_samples_leaf usually matter most.

    IntermediateQuick answer1 min read
  27. Why is KNN slow at prediction time and poor with many features?

    KNN must compare a query against every stored training row, so prediction cost grows with data size. With many features, distances between points become almost equal, so 'nearest' stops meaning much. This is the curse of dimensionality.

    IntermediateQuick answer1 min read