FastPrepBernoulli Naive Bayes Classifier
Problems/Bernoulli Naive Bayes Classifier

Bernoulli Naive Bayes Classifier

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report names a Bayesian classifier but does not say what its feature model or probability rules are. Here we use Bernoulli naive Bayes with add-one smoothing, and we set a rule for numerical ties. Our version is about 60% the same as the reported question.

For this exercise, implement a Bernoulli naive Bayes classifier directly, without a machine-learning library. The training matrix train has binary feature values and one integer class label in labels per row. All classes 0..numClasses-1 occur in training.

Fit the following fixed model. Let count[c] be the number of training rows labeled c, and ones[c][j] the number of those rows whose feature j is 1.

  • Class prior: prior[c] = count[c] / n.
  • Add-one conditional smoothing: p[c][j] = (ones[c][j] + 1) / (count[c] + 2).
  • For a query x, its natural-log score for class c is ln(prior[c]) + sum_j(x[j]*ln(p[c][j]) + (1-x[j])*ln(1-p[c][j])).

For each row of queries, find the maximum class score and return the smallest class label whose score differs from that maximum by at most 10^-10. This numerical tie rule is part of the exercise contract. Return labels in query order; an empty query matrix returns an empty array. Do not smooth the class prior or transform the already-binary features.

Function

bernoulliClassify(train: int[][], labels: int[], numClasses: int, queries: int[][]) → int[]

Examples

Example 1

train = [[0],[1]]labels = [0,1]numClasses = 2queries = [[0],[1]]return = [0,1]

Each class has one example; smoothed likelihood favors the class matching the query.

Example 2

train = [[0,1],[1,0],[0,1],[1,0]]labels = [0,1,0,1]numClasses = 2queries = [[0,1],[1,0],[1,1],[0,0]]return = [0,1,0,0]

The first two queries favor their matching class. The last two have equal scores and choose label 0.

Example 3

train = [[1],[1],[1],[0]]labels = [0,0,0,1]numClasses = 2queries = [[0],[1]]return = [1,0]

The empirical prior favors class 0, but a zero query still favors class 1: 1/6 is larger than 3/20.

Constraints

  • 2 <= numClasses <= 10 and numClasses <= train.length <= 500.
  • labels.length = train.length; every class is represented.
  • Every training row and query row has the same feature count d, where 1 <= d <= 50.
  • Every matrix entry is 0 or 1, and 0 <= labels[i] < numClasses.
  • 0 <= queries.length <= 200.
  • For every query, each class has exactly the maximum mathematical log score or trails that maximum by at least 10^-8.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
  • Maximize the Even-Odd Index Product GapInfosys · ONSITE INTERVIEWInfosys interview questions
  • Squirrel Nut Collection Shortest PathAmazon · ONSITE INTERVIEW
  • Expected Value of Modified DiceJane Street · ONSITE INTERVIEWJane Street interview questions
  • Linear Layer Forward and Backward PassesMeta · ONSITE INTERVIEWMeta interview questions
  • Product Total With the Cheapest PromotionInstacart · ONSITE INTERVIEWInstacart interview questions
  • Reservoir Sampling with Recorded DrawsStackAdapt · ONSITE INTERVIEWStackAdapt interview questions
  • Koko Eating BananasTekion · ONSITE INTERVIEWTekion interview questions
  • Dense Vector Dot ProductCerebras · ONSITE INTERVIEW
  • Sliding-Window Averages as Reduced FractionsCoinbase · ONSITE INTERVIEWCoinbase interview questions

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[] bernoulliClassify(int[][] train, int[] labels, int numClasses, int[][] queries) {
        return new int[0];
    }
}
Checking account…