Naive Bayes — ML Technical Interview Questions & Answers
A fast probabilistic classifier based on Bayes' theorem.
Naive Bayes
- Simple meaning: Naive Bayes uses probability: given these words (or features), which class is more likely? “Naive” = it pretends features are independent. Often still works for spam.
- Picture: Email with “lottery” and “urgent”. P(spam | words) vs P(ham | words). Multiply word likelihoods (with smoothing so a new word is not probability 0).
- sklearn: MultinomialNB for counts (text). GaussianNB for numeric bells. Fast to train. Great baseline for text.
- Trap: zero probability killing the product — use Laplace smoothing. Trap: using it when features are strongly tied and you need a careful model — still start here for spam homework.
Technical Interview Tip: Spam vs ham with word probabilities. Smooth so nothing is zero.
Code Examples for Naive Bayes
Naive Bayes — working example
def majority_vote(labels):
return max(set(labels), key=labels.count)
neighbors = [1, 1, 0, 1, 0]
print(majority_vote(neighbors)) # kNN-style vote
# Logistic idea: score → probability via sigmoid
def sigmoid(z):
return 1 / (1 + 2.718281828 ** (-z))
print(round(sigmoid(0.0), 3), round(sigmoid(2.0), 3))Naive Bayes — classifiers map features to labels; kNN votes neighbors; logistic uses a sigmoid probability.
How to Explain Naive Bayes to an Interviewer
Why ‘naive’?
Assumes features are independent
Where is it strong?
Text / spam Fast baseline