Senior interview questionAsked at Pinterest · interview write-up
Implement a Naive Bayes classifier from scratch with fit and predict, estimating priors and likelihoods and working in log space.
Model answer
Multinomial Naive Bayes over count features, with Laplace smoothing.
import numpy as np
class MultinomialNB:
def __init__(self, alpha=1.0):
self.alpha = alpha
def fit(self, X, y):
# X (N, V) counts, y (N,) labels
self.classes = np.unique(y)
n, V = len(self.classes), X.shape[1]
self.log_prior = np.empty(n)
self.log_lik = np.empty((n, V))
for i, c in enumerate(self.classes):
Xc = X[y == c]
self.log_prior[i] = np.log(len(Xc) / len(X))
cnt = Xc.sum(0) + self.alpha
self.log_lik[i] = np.log(cnt / cnt.sum())
return self
def predict(self, X):
s = X @ self.log_lik.T + self.log_prior
return self.classes[s.argmax(1)]
Interviewers check you sum log-probabilities instead of multiplying, which underflows; that smoothing stops one unseen feature zeroing a class; and that you can name the variant to swap in: Gaussian for continuous features, Bernoulli for binary. For probabilities, normalize the scores with log-sum-exp.
Practise the rest
35 Training questions, 492 in all, each with a model answer. Free.
More from Pinterest interview questions.
More Training interview questions
- Implement masked cross-entropy in NumPy that stays numerically stable, with label smoothing and a temperature.
- Build a pipeline that finds math documents in web crawls and scores their quality.
- Explain KL divergence for language models: which direction is which, what happens when supports don't match, and how it's used as a regulariser.
- When would you use mean squared error versus cross-entropy? And what is LoRA?
- Explain Adam and its bias correction, and why AdamW decouples weight decay.
- Explain policy types in RL and modern policy-gradient methods such as TRPO, PPO and GAE, then compare linear attention and grouped-query attention.