Skip to content
DeepForgeby svx
All problems
MediumProbabilitypr-123

Binomial Exactly k Balls in a Bin

Problem

Balls are thrown independently and uniformly into bins bins. Return the probability a fixed bin receives exactly k of the balls:

P = C(balls, k) * (1 / bins)^k * (1 - 1 / bins)^(balls - k).

Return 0.0 for invalid arguments.

Hint

Condition on one bin; its load is Binomial(balls, 1 / bins).

Test cases

case 1
in: [10,10,0]
expected: 0.3486784401000001
case 2
in: [2,2,1]
expected: 0.5
case 3
in: [3,6,0]
expected: 0.5787037037037038
case 4
in: [1,0,0]
expected: 0
Starter codePython
import math


def bin_exactly_k_balls(balls, bins, k):
    # Your code here
    pass

4 test cases · run your code against them in the editor