FastPrepTwo Colors in Replacement Draws

Two Colors in Replacement Draws

IMC Trading logoIMC Trading● EasyNEW GRADOA
Learn

Problem statement

A bag contains colorCount equally likely colors, including distinct red and green colors. Draw a marble drawCount times, replacing it after every draw. Find the probability that both red and green appear at least once.

For this exercise, assume the replacement draws are independent, and generalize the reported five-color, four-draw case to the supplied counts. Return the probability rounded to the nearest hundredth as a numeric decimal. For this exercise, assume exact halfway values round upward, so 0.125 becomes 0.13.

Zero draws give probability zero. For this exercise, assume relative-or-absolute numeric tolerance 1e-9 for the already rounded result. Complete bothColorsProbability(colorCount, drawCount).

Function

bothColorsProbability(colorCount: int, drawCount: int) → double

Examples

Example 1

colorCount = 5drawCount = 4return = 0.31

The reported case has 5^4 = 625 draw sequences. Exactly 625 - 2×256 + 81 = 194 contain both red and green. The probability 0.3104 rounds to 0.31.

Example 2

colorCount = 2drawCount = 2return = 0.50

Of the four sequences, red-green and green-red contain both distinguished colors.

Example 3

colorCount = 4drawCount = 2return = 0.13

Exactly two of sixteen sequences contain both colors. The exact probability is 0.125; the declared half-up convention gives 0.13.

Constraints

  • For this exercise, assume 2 <= colorCount <= 10 and 0 <= drawCount <= 12.
  • Red and green are two different colors, each with probability 1/colorCount on each draw.
  • All exact sequence counts and the integer half-up rounding numerator fit a signed 64-bit integer under these bounds.

More IMC Trading problems

See IMC Trading hiring insights
public double bothColorsProbability(int colorCount, int drawCount) {
    // Write your solution here.
}
colorCount5
drawCount4
expected0.31
Checking account…