FastPrepExpected Fishing Catch

Expected Fishing Catch

IMC Trading logoIMC Trading● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

A fishing trip has a finite set of possible outcomes. Outcome i produces catches[i] fish. For this exercise, assume its probability is weights[i] / sum(weights), where weights are nonnegative integers with a positive total.

Return the expected number of fish caught on one trip. Repeated catch amounts are allowed and still contribute their separate probability weights. An outcome with zero weight contributes nothing.

For this exercise, assume the numeric outcome table is supplied as two arrays; the original report states the expected-catch calculation but does not provide that table. Complete expectedFishingCatch(catches, weights). An answer is accepted when its absolute error is at most 1e-7 * max(1, abs(expected)), using a relative-or-absolute tolerance of 1e-7.

Function

expectedFishingCatch(catches: int[], weights: int[]) → double

Examples

Example 1

catches = [0,2,5]weights = [1,2,1]return = 2.25

The weighted catch sum is 0+4+5=9 and the total weight is 4, so the expectation is 2.25. This table is an authored practice example.

Example 2

catches = [3]weights = [8]return = 3.0

The only possible catch is three fish, so its probability is one and the expectation is three.

Example 3

catches = [0,0]weights = [1,1]return = 0.0

Every possible outcome catches zero fish.

Example 4

catches = [1,2]weights = [1,2]return = 1.6666666666666667

The exact expectation is (1+4)/3=5/3; retain the fractional result.

Constraints

  • For this exercise, assume the arrays have equal lengths from 1 through 100.
  • 0 <= catches[i], weights[i] <= 1000000, and at least one weight is positive.
  • Return a double expectation; do not round to a whole fish.
  • For this exercise, assume inputs are not modified.

More IMC Trading problems

See IMC Trading hiring insights
public double expectedFishingCatch(int[] catches, int[] weights) {
    // Write your solution here.
}
catches[0,2,5]
weights[1,2,1]
expected2.25
Checking account…