KatInterview

Probability of Keeping Majority Red Items

Math & Bit ManipulationAuto-graded · 25 test cases
EasyAuto-gradedMath & Bit Manipulationmathcombinatoricsprobability

Last updated

Calculates the probability that at least two of three kept items are red when green items are returned upon selection.

A container contains three types of items: red red items, blue blue items, and green green items.

Items are drawn one at a time uniformly at random according to the following rules:

  • If a green item is drawn, it is returned to the container.
  • If a red or blue item is drawn, it is kept (removed from the container).

Drawing continues until exactly 3 items have been kept.

Return the probability that at least 2 of the 3 kept items are red.

Edge Case Handling

  • If there are fewer than 2 red items (red < 2), it is impossible to have at least 2 red items kept, so return 0.0.
  • If the total number of non-green items is less than 3 (red + blue < 3), it is impossible to collect 3 kept items, so return 0.0.

Constraints

  • $0 \le \text{red}, \text{blue}, \text{green} \le 10^4$

Examples

Example 1

Input: probability_at_least_two_red(5, 5, 5)

Output: 0.5

Example 2

Input: probability_at_least_two_red(10, 5, 5)

Output: 0.7582417582417582

Hints

Hint 1

Drawing a green item leaves the count of red and blue items unchanged. Does drawing green items affect the relative chances of drawing red vs blue?

Hint 2

Conditioned on drawing a non-green item, what is the probability of selecting a red item versus a blue item?

Hint 3

Since green draws do not alter the pool of red and blue items, the problem reduces to sampling 3 items without replacement from red and blue items only.