Bayesian Scenario Classifier
Updated
Problem statement
In this exercise, you write a Bayesian classifier that uses a given model. Each observation is a list of binary features. There are c classes labeled 0..c-1 and d features.
classWeights[k]is the prior weight of classk, and it is never negative. The prior of a class is its weight divided by the sum of all weights.positivePercentages[k][j]is a whole-number percentage. It is the chance that featurejis1in classk. The chance that it is0is the complement: whatever is left of the whole.- Given the class, the features are independent of each other (conditionally independent). Use the given chances exactly as they are, with no smoothing.
For each row x of observations, pick the class with the largest posterior probability. You get the same answer by comparing exact whole-number scores. The score of class k is classWeights[k] * product_j factor[k][j], where product_j means you multiply over all features j. The factor is positivePercentages[k][j] if x[j] = 1, and 100 - positivePercentages[k][j] otherwise.
If the largest score is positive, return the smallest class label that has that score. If every score is zero, return -1, because the observation has zero probability under this model. Return one label per observation, in input order. If the observation matrix is empty, return an empty array.
Function
classifyScenarios(classWeights: int[], positivePercentages: int[][], observations: int[][]) → int[]Examples
Example 1
classWeights = [1,1]positivePercentages = [[80,20],[20,80]]observations = [[1,0],[0,1],[1,1],[0,0]]return = [0,1,0,0]For [1,0], the scores are 6400 and 400, so the first class wins. The row with the values swapped picks class 1 for the same reason. The other two rows give both classes the same score, so the tie goes to the smaller label, class 0.
Example 2
classWeights = [3,1]positivePercentages = [[20],[80]]observations = [[1],[0]]return = [1,0]For the observation with a one, the scores are 60 and 80, so class 1 wins. For the observation with a zero, the scores are 240 and 20, so class 0 wins. The prior weights change the scores in both cases.
Example 3
classWeights = [1,1]positivePercentages = [[100,0],[0,100]]observations = [[1,1],[0,0],[1,0],[0,1]]return = [-1,-1,0,1]The first two observations score zero in every class, so each gets the label for impossible observations. Each of the last two observations matches the only pattern one class allows, so it gets that class.
Constraints
2 <= c <= 10and1 <= d <= 8.classWeights.length = c; each weight is in0..100, with at least one positive weight.positivePercentageshascrows of lengthd; each entry is in0..100.0 <= observations.length <= 500; every row has lengthdand contains only0or1.- Scores are compared exactly. A score is at most
10^18, so use signed64-bit arithmetic where needed.
More Amazon Onsite Interview questions
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximum Earnings Across WarehousesONSITE INTERVIEW · Seen Nov 2026
- Priority Message BufferONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW
- Count Subarrays with a Perfect-Square ProductMicrosoft · OAMicrosoft interview questions
- Repeated Mirror XORMathWorks · OAMathWorks interview questions
- Minimum Power-of-Two Removal OperationsIBM · OAIBM interview questions
- Minimum Power-of-Two Removal OperationsVisa · OAVisa interview questions
- Find the Missing Number from 1 to nGEP · OA
- Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
- Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Find the Two Unique NumbersZomato / Eternal · ONSITE INTERVIEWZomato / Eternal interview questions
- Maximize the Even-Odd Index Product GapInfosys · ONSITE INTERVIEWInfosys interview questions
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)