FastPrepFit and Predict a Naive Bayes Sentiment Model
Problems/Fit and Predict a Naive Bayes Sentiment Model

Fit and Predict a Naive Bayes Sentiment Model

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A 2024 report on the ML round of a Google ML SWE III loop says the candidate had to code the training and prediction steps of a sentiment model whose math was described to them. The interviewer then asked about other options, such as deep-learning models, byte-pair tokenizers, and TF-IDF weighting.

The report does not share the model itself. Here, the model is multinomial naive Bayes with add-one smoothing. Unknown test words are skipped, and predict returns the probability that a text is positive, checked within 10^-9. The follow-ups are discussed in the editorial and are not checked. The limits are our choice.

Our version is about 55% the same as the reported question.

Implement a small sentiment classifier: fit it on labeled texts, then predict how likely each new text is to be positive. Label 1 means positive and 0 means negative. Use this model, a multinomial naive Bayes model with add-one smoothing:

  • Fit. Let N_c be the number of training texts with label c, and n the total number of training texts. Let count(w, c) be how many times word w appears in all training texts with label c, counting repeats, and total(c) the number of words in those texts. Let V be the number of distinct words in all training texts. Then P(c) = N_c / n and P(w | c) = (count(w, c) + 1) / (total(c) + V).
  • Predict. For a text, multiply P(c) by P(w | c) once for every word occurrence in the text, skipping words that never appear in training. Call the results score(0) and score(1). Return score(1) / (score(0) + score(1)).

Return one probability per test text, in order. Answers within 10^-9 of the expected value are accepted.

Function

sentimentProbabilities(trainTexts: String[], trainLabels: int[], testTexts: String[]) → double[]

Examples

Example 1

trainTexts = ["good movie","great fun","bad movie","boring and bad"]trainLabels = [1,1,0,0]testTexts = ["good fun","bad movie","unknown words"]return = [0.8263988522238164,0.28402366863905326,0.5]

The vocabulary has 7 words: good, movie, great, fun, bad, boring, and. Positive texts have 4 words and negative texts have 5. Both priors are 2/4.

  • good fun: positive score 0.5 * (2/11) * (2/11), negative score 0.5 * (1/12) * (1/12). The probability is about 0.8264.
  • bad movie: positive 0.5 * (1/11) * (2/11), negative 0.5 * (3/12) * (2/12), giving about 0.2840.
  • unknown words: neither word is in the vocabulary, so only the equal priors remain and the answer is 0.5.

Example 2

trainTexts = ["yes","yes","yes","no"]trainLabels = [1,1,1,0]testTexts = ["yes no","no"]return = [0.6835443037974683,0.47368421052631576]

The priors are 3/4 and 1/4. With V = 2, the positive class has 3 words, so P(yes | +) = 4/5 and P(no | +) = 1/5; the negative class has 1 word, so P(yes | -) = 1/3 and P(no | -) = 2/3. For yes no: (3/4)(4/5)(1/5) = 0.12 against (1/4)(1/3)(2/3) = 1/18, so about 0.6835. For no: (3/4)(1/5) = 0.15 against (1/4)(2/3) = 1/6, so about 0.4737.

Constraints

  • 2 <= trainTexts.length == trainLabels.length <= 200
  • Each trainLabels[i] is 0 or 1, and both labels appear.
  • 1 <= testTexts.length <= 50
  • Every text has 1 to 30 words separated by single spaces, with no leading or trailing spaces, and has at most 330 characters. Every word has 1 to 10 lowercase English letters.

More Google Onsite Interview questions

  • 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
  • Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Filesystem Entity Total SizeONSITE INTERVIEW · Seen Nov 2026
  • Longest Subarray Within an Absolute-Difference LimitONSITE INTERVIEW · Seen Nov 2026
  • Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Reconstruct Digits from Jumbled English WordsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Text Embedding and Vector SearchNike, Inc. · PHONE SCREEN · ONSITE INTERVIEWNike, Inc. interview questions
  • Fraction to Recurring DecimalGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Rank Bipedal Dinosaurs by SpeedMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Reorderable Task Scheduler With CooldownWaymo · PHONE SCREENWaymo interview questions
  • Minimum Anagram PeriodVisa · OAVisa interview questions
  • Maximum Equal Parts for PrefixesAmazon · OA
  • Per-Segment SpeedingOnePay · PHONE SCREENOnePay interview questions
  • Find Accounts with Negative Converted BalancesStripe · PHONE SCREENStripe interview questions
  • Minimum Anagram PeriodWells Fargo · OAWells Fargo interview questions

Interview reports that mention this question

  • Google SWE III ML Loops in India: 10 Experiences (Part 2 of 3)

Google interview experiences

  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
  • Google AI Coding Interview and AI OA: 2 Experiences
  • Google SWE Intern Interview Results Timeline: 13 Experiences
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public double[] sentimentProbabilities(String[] trainTexts, int[] trainLabels, String[] testTexts) {
        // Write your code here.
    }
}
Checking account…