← Pinterest Interview Insights

Pinterest·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
Jun 2026

Summary

Pinterest ML Engineer interview with a from-scratch Naive Bayes implementation question. Pretty focused on the math and numerical stability side of things, not just slapping sklearn on it.

Questions Asked (1)

Q1

Implement a Naive Bayes classifier from scratch using NumPy. Your implementation should have a fit method to estimate class priors and likelihood parameters, and a predict method to compute posteriors and return the most likely class. You also need to specify which variant you're implementing (Gaussian, Multinomial, or Bernoulli) and handle numerical stability.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I went with Gaussian NB since the prompt mentioned continuous features and that felt like the safest bet.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explicitly stating which Naive Bayes variant you'll implement (e.g., Gaussian) and why it suits the problem. Then outline the fit method: compute class priors from label frequencies and estimate likelihood parameters (mean and variance per feature per class). Finally, describe the predict method: compute log-posteriors using the log-likelihood and log-prior, apply numerical stability tricks (log-sum-exp), and return the argmax.

Pro tip: Mention that you'll use log probabilities to avoid underflow and that you'll add a small epsilon to variances to prevent division by zero. Also, note that for Gaussian NB, you can vectorize computations across classes for efficiency.

1. Choose and justify the variant

Select Gaussian, Multinomial, or Bernoulli based on the data type (continuous, count, or binary). Briefly explain why it's appropriate for the problem.

2. Implement fit: priors and likelihood parameters

Compute class priors as the proportion of each class in the training data. For Gaussian, compute mean and variance per feature per class; for Multinomial, compute feature probabilities with Laplace smoothing; for Bernoulli, compute probabilities of each feature being 1.

3. Implement predict: compute log-posteriors

For each test sample, compute the log-prior plus the sum of log-likelihoods for each class. Use log-sum-exp to normalize if needed, and return the class with the highest log-posterior.

4. Handle numerical stability

Use log probabilities to prevent underflow. Add a small epsilon to variances (Gaussian) or use Laplace smoothing (Multinomial/Bernoulli) to avoid zero probabilities.

5. Vectorize and test

Vectorize operations using NumPy for efficiency. Test on a small dataset and compare with scikit-learn's implementation to validate correctness.

Key Points to Mention

  • Choice of variant and its assumptions (e.g., Gaussian assumes features are normally distributed within each class).
  • Class priors estimation: P(y) = count(y) / total_samples.
  • Likelihood estimation: for Gaussian, mean and variance per feature per class; for Multinomial, feature counts with Laplace smoothing; for Bernoulli, presence/absence probabilities.
  • Numerical stability: log probabilities, log-sum-exp trick, epsilon for variance, Laplace smoothing.
  • Vectorization with NumPy for efficient computation (e.g., broadcasting for mean/variance).
  • Evaluation: accuracy, confusion matrix, and comparison with scikit-learn.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.