Fit and Predict a Naive Bayes Sentiment Model
Updated
Problem statement
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_cbe the number of training texts with labelc, andnthe total number of training texts. Letcount(w, c)be how many times wordwappears in all training texts with labelc, counting repeats, andtotal(c)the number of words in those texts. LetVbe the number of distinct words in all training texts. ThenP(c) = N_c / nandP(w | c) = (count(w, c) + 1) / (total(c) + V). - Predict. For a text, multiply
P(c)byP(w | c)once for every word occurrence in the text, skipping words that never appear in training. Call the resultsscore(0)andscore(1). Returnscore(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 score0.5 * (2/11) * (2/11), negative score0.5 * (1/12) * (1/12). The probability is about 0.8264.bad movie: positive0.5 * (1/11) * (2/11), negative0.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