Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Naive Bayes From Scratch
00:00
5 left

Naive Bayes From Scratch

MediumPython

Problem

Moloco's ad-ranking systems may use compact categorical features such as device type, traffic source, and placement. Implement a categorical Naive Bayes classifier from scratch to predict a label for each test example.

Given training feature rows train_X, training labels train_y, and unlabeled rows test_X, return one predicted label for every test row. Assume feature columns are categorical and represented by hashable values.

Use maximum a posteriori classification:

P(class | features) ∝ P(class) × ∏ P(feature_i | class)

Estimate conditional probabilities with Laplace smoothing:

P(value | class, feature_i) = (count(value, class, feature_i) + 1) / (count(class) + number_of_training_values_in_feature_i)

Perform calculations in log space to avoid numerical underflow. If a test value was never observed in a feature column, treat its count as zero while using that column's training vocabulary size in the denominator. Preserve the order of first appearance when breaking prediction ties.

Formal Specification

Implement train_predict_naive_bayes(train_X, train_y, test_X). train_X is a non-empty list of equal-length rows, train_y contains one hashable label per training row, and test_X contains rows with the same number of features. Return a list of predicted labels.

Constraints

  • 1 <= len(train_X) <= 10^4
  • 1 <= len(train_X[0]) <= 100
  • len(train_y) == len(train_X)
  • 0 <= len(test_X) <= 10^3
  • Every training and test row has the same feature count
  • Feature values and labels are hashable

Function Signature

def train_predict_naive_bayes(train_X, train_y, test_X):
Interviewer

Your question is Naive Bayes From Scratch. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.