Implement Naive Bayes classifier from scratch

Read the full interview experience this question came from →

Quick Overview

This question evaluates understanding of probabilistic classification, specifically competence in estimating class priors, feature likelihood parameters, and computing posterior scores for Naive Bayes variants.

Implement Naive Bayes classifier from scratch

Company: Pinterest

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Online Assessment

Implement a **Naive Bayes** classifier from scratch (you may use NumPy). Write a class with: - `fit(X, y)`: estimate class priors and feature likelihood parameters. - `predict(X)`: compute posteriors (or unnormalized log-posteriors) and output the most likely class. Specify which variant you implement (e.g., **Multinomial Naive Bayes** for count features, **Bernoulli NB** for binary features, or **Gaussian NB** for continuous features). Your implementation should clearly compute: - prior \(P(y=c)\) - likelihood parameters for \(P(x\mid y=c)\) - posterior scoring using Bayes’ rule with numerical stability (typically log-probabilities).

Overview: This question evaluates understanding of probabilistic classification, specifically competence in estimating class priors, feature likelihood parameters, and computing posterior scores for Naive Bayes variants.

Read the full Pinterest Machine Learning Engineer interview experience this question came from

|Home/Machine Learning/Pinterest
Pinterest logo
Pinterest
Feb 9, 2026
hardMachine Learning EngineerOnline AssessmentMachine Learning
8
0

Implement a Naive Bayes classifier from scratch (you may use NumPy).

Write a class with:

  • fit(X, y) : estimate class priors and feature likelihood parameters.
  • predict(X) : compute posteriors (or unnormalized log-posteriors) and output the most likely class.

Specify which variant you implement (e.g., Multinomial Naive Bayes for count features, Bernoulli NB for binary features, or Gaussian NB for continuous features). Your implementation should clearly compute:

  • prior P(y=c)P(y=c)
  • likelihood parameters for P(x∣y=c)P(x\mid y=c)
  • posterior scoring using Bayes’ rule with numerical stability (typically log-probabilities).
Loading comments...