← Pinterest Interview InsightsMachine Learning Engineer

Pinterest·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
Jun 2026

Summary

Pinterest ML Engineer interview that was basically one meaty coding question: build Naive Bayes from scratch. No tricks, just whether you actually understand the math and can write clean NumPy.

Questions Asked (1)

Q1

Implement a Naive Bayes classifier from scratch using NumPy. Your implementation should include a fit method that computes class priors and per-class feature likelihoods, a predict method that uses log-space computation, and proper handling of numerical stability. Be prepared to discuss the conditional independence assumption and when it breaks down.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The fit/predict split was fine, I've done it before.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem type (e.g., multinomial or Gaussian Naive Bayes) and then outline the mathematical formulas for priors and likelihoods. Implement fit and predict methods with log-space computations to avoid underflow, and discuss the conditional independence assumption and its limitations.

Pro tip: Mention that you would add a small epsilon to probabilities to handle zero counts (Laplace smoothing) and explain how log-sum-exp can be used for numerical stability in predict. This shows practical awareness beyond basic implementation.

1. Clarify assumptions and data type

Ask whether features are continuous (Gaussian NB) or discrete (Multinomial/Bernoulli NB) and confirm the expected input format. This determines the likelihood computation.

2. Implement fit method

Compute class priors (log P(class)) and per-class feature likelihoods (e.g., mean and variance for Gaussian, or feature counts for multinomial). Apply Laplace smoothing if needed.

3. Implement predict method in log-space

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

4. Address numerical stability

Explain how log-space avoids underflow, and mention techniques like adding epsilon to probabilities or using log1p for small values. Discuss handling of zero probabilities via smoothing.

5. Discuss conditional independence assumption

Explain that Naive Bayes assumes features are independent given the class, which is often violated in practice (e.g., correlated features). Mention that it still works well for text classification but can be problematic for tasks like image recognition where features are highly correlated.

Key Points to Mention

  • Difference between Gaussian, Multinomial, and Bernoulli Naive Bayes and when to use each.
  • Laplace smoothing (additive smoothing) to handle zero counts and its effect on probabilities.
  • Log-space computation to prevent underflow and the use of log-sum-exp for normalization.
  • The conditional independence assumption and examples where it breaks down (e.g., duplicate features, correlated features).
  • Time and space complexity: O(n_features * n_classes) for training and prediction, making it efficient for large datasets.
  • How to handle missing values or unseen features during prediction.

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