Interview Q&A / Machine Learning

IntermediateQuick answer1 min readUpdated 3 Oct 2026

How does Naive Bayes classify an email as spam? Work through a small example.

SHORT ANSWER

It uses Bayes' theorem: posterior is proportional to prior times the product of each word's likelihood given the class. Compute that score for spam and for not-spam, then normalise. The class with the higher posterior wins.

Explain it simply

A mail clerk notices that "free" and "offer" show up often in junk mail and rarely in genuine mail. When a new email contains both words, the clerk's suspicion rises. Naive Bayes turns that habit into arithmetic.

Example

Known from training data:

text
P(spam) = 0.4          P(ham) = 0.6
P(offer | spam) = 0.5  P(offer | ham) = 0.05
P(free  | spam) = 0.4  P(free  | ham) = 0.10

New email contains "offer" and "free".

text
score(spam) = 0.4 x 0.5  x 0.4  = 0.08
score(ham)  = 0.6 x 0.05 x 0.10 = 0.003

P(spam | words) = 0.08 / (0.08 + 0.003) = 0.08 / 0.083 = 0.964

So about 96.4% spam.

python
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline
model = make_pipeline(CountVectorizer(), MultinomialNB())

In practice, implementations add log probabilities instead of multiplying, to avoid numbers so small that the computer rounds them to zero.

What to say in the interview

"Naive Bayes applies Bayes' theorem with the assumption that features are independent given the class. It multiplies the class prior by each feature's likelihood, compares the classes and normalises. It is fast, works with little data, and is a strong baseline for text."

Common mistakes

  • Forgetting the prior.
  • Multiplying many tiny probabilities instead of summing logs.

#naive-bayes#bayes-theorem#spam

Related questions

Practise more Machine Learning questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Machine Learning quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.