← Pinterest Interview Insights

Pinterest·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
May 2026

Summary

Pinterest ML Engineer interview that came down to implementing a Naive Bayes classifier from scratch. Pretty focused session, no fluff, they wanted to see if you actually understood the math behind something you've probably just called from sklearn a hundred times.

Questions Asked (1)

Q1

Implement a Naive Bayes classifier from scratch. It should handle both Gaussian (continuous features) and Multinomial (word-count features) variants, support a fit method that computes class priors and per-class likelihoods, and a predict method that returns the argmax of the log-posterior across classes. Use log-space arithmetic and Laplace smoothing where needed.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

First thing they asked was which variant I wanted to implement and I kind of fumbled that.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and outlining the mathematical foundations of Naive Bayes, then design a class hierarchy with a base class and two subclasses for Gaussian and Multinomial variants. Implement fit to compute class priors and per-class likelihood parameters, and predict to compute log-posteriors using log-space arithmetic and Laplace smoothing, returning the argmax.

Pro tip: Emphasize numerical stability by using log-sum-exp for normalization and explain how Laplace smoothing prevents zero probabilities, especially for unseen words in Multinomial NB. Also, mention that for Gaussian NB, you can use the log of the Gaussian PDF directly to avoid underflow.

1. Clarify requirements and assumptions

Confirm the input data format, expected output, and any constraints (e.g., handling of unseen features). Discuss the need for log-space arithmetic and Laplace smoothing.

2. Design class structure and interfaces

Propose a base class NaiveBayes with fit and predict methods, and subclasses GaussianNB and MultinomialNB. Define parameters to store (priors, means, variances, feature log probabilities).

3. Implement fit method

For Gaussian: compute class priors, per-class means and variances (with smoothing if needed). For Multinomial: compute class priors and per-class feature counts with Laplace smoothing to get log probabilities.

4. Implement predict method

For each sample, compute log-posterior for each class by summing log-prior and log-likelihoods (using Gaussian log PDF or multinomial log probabilities). Return the class with the highest log-posterior.

5. Discuss trade-offs and optimizations

Mention computational complexity, memory usage, and potential improvements like vectorization, handling missing values, or using log-sum-exp for normalization if probabilities are needed.

Key Points to Mention

  • Log-space arithmetic to avoid underflow and improve numerical stability
  • Laplace smoothing (additive smoothing) for Multinomial NB to handle zero counts
  • Gaussian NB likelihood: log of Gaussian PDF with mean and variance per feature per class
  • Class priors computed as log of class frequencies (with smoothing if necessary)
  • Argmax of log-posterior is equivalent to argmax of posterior due to monotonicity of log
  • Vectorization for efficiency when computing likelihoods across multiple samples

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