Binomial Distribution Probability

Medium
Machine Learning

Write a Python function to calculate the probability of achieving exactly k successes in n independent Bernoulli trials, each with probability p of success, using the Binomial distribution formula.

Examples

Example 1:
Input: n = 6, k = 2, p = 0.5
Output: 0.23438
Explanation: We want the probability of getting exactly 2 successes in 6 trials with 50% success rate. The binomial coefficient C(6,2) = 15, and the probability calculation gives 15 × 0.25 × 0.0625 = 0.23438.

Starter Code

import math

def binomial_probability(n: int, k: int, p: float) -> float:
    """
    Calculate the probability of exactly k successes in n Bernoulli trials.
    
    Args:
        n: Total number of trials
        k: Number of successes
        p: Probability of success on each trial
    
    Returns:
        Probability of k successes
    """
    # Your code here
    pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews