Which Factory Made the Red Widgets
Updated
Problem statement
Two factories make widgets, and every widget is either red or black. At Factory A, each widget is red with probability percentA percent. At Factory B, each widget is red with probability percentB percent. The color of each widget does not depend on any other widget.
One of the two factories is picked at random, each with probability 1/2. Then k widgets are taken from that factory, and all k of them are red.
Return the probability that the widgets came from Factory A. Answers within 10^-9 of the exact value are accepted.
Function
factoryAProbability(percentA: int, percentB: int, k: int) → doubleExamples
Example 1
percentA = 40percentB = 80k = 2return = 0.2This is the interview case. From Factory A, two red widgets in a row have probability 0.4 * 0.4 = 0.16. From Factory B, the probability is 0.8 * 0.8 = 0.64. Each factory is picked half the time, so two red widgets happen with probability 0.5 * 0.16 + 0.5 * 0.64 = 0.4, and 0.5 * 0.16 = 0.08 of that comes from Factory A. The answer is 0.08 / 0.4 = 0.2.
Example 2
percentA = 40percentB = 80k = 1return = 0.3333333333333333Now only one widget is seen. The answer is 0.5 * 0.4 / (0.5 * 0.4 + 0.5 * 0.8) = 0.2 / 0.6 = 1/3. Seeing a second red widget in Example 1 makes Factory A even less likely.
Example 3
percentA = 90percentB = 30k = 3return = 0.9642857142857143Factory A makes red widgets more often. Three red widgets have probability 0.9^3 = 0.729 from Factory A and 0.3^3 = 0.027 from Factory B. The answer is 0.729 / (0.729 + 0.027) = 27/28, about 0.9642857143.
Constraints
1 <= percentA <= 1001 <= percentB <= 1001 <= k <= 1000
More Susquehanna International Group (SIG) Online Assessment questions
- Catch Fish with Reusable BaitsOA · Seen Nov 2026
- Bold Play to Reach a GoalOA · Seen Nov 2026
- Cats, Truth and Who Has MoreOA · Seen Nov 2026
- Counting Cows by Their LegsOA · Seen Nov 2026
- Enough Muffins for the LineOA · Seen Nov 2026
- Expected Consecutive Pairs Among Chosen NumbersOA · Seen Nov 2026
- Four-by-Four Boundary GridOA · Seen Nov 2026
- Frog Paths Without Long RunsOA · Seen Nov 2026
- Higher Roll, or Lose the SumOA · Seen Nov 2026
- Is a Popular Story Fake?OA · Seen Nov 2026
Similar questions at other companies
- Count Valid Square PairsAuxoAI · OA
- Server InvestmentRippling · OARippling interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Identification Number Check DigitAbridge · OAAbridge interview questions
- Count Substrings With Identical CharactersVirtu Financial · OAVirtu Financial interview questions
- Next Pascal Row CoefficientIntuit · OAIntuit interview questions
- Digit-Sum Sequence Join PointOpenAI · OAOpenAI interview questions
- Minimum Testers to Find the Poisoned BottleWalleye Capital · OA
- Address on Same PageNVIDIA · OANVIDIA interview questions
- Preorder of a Special Binary TreeNielsen · OA