Compute Multi-User Feature Adoption Metrics
Math & Bit ManipulationAuto-graded · 20 test casesLast updated
Given population size and individual adoption probability, calculate key expectation, probability, and conditional probability metrics under Bernoulli assumptions.
Consider a system of $N$ potential users. Each user independently opts into a new product feature with probability $p$.
Given $N$ and $p$, write a function to calculate the following three values:
- Expected number of adopters: The expected count of users who opt into the feature across all $N$ users.
- Probability of at least one adopter: The probability that 1 or more users among the $N$ total users opt into the feature. (If $N = 0$, this probability is $0.0$).
- Conditional probability for two designated users: Given two specific users in the system, what is the probability that both adopt the feature, conditional on at least one of these two specific users adopting it? (If $p = 0.0$, output $0.0$).
Return these three values in a list of floating-point numbers: [expected_adopters, prob_at_least_one, prob_both_given_at_least_one].
Constraints
- $0 \le N \le 10^6$
- $0.0 \le p \le 1.0$
Floating-Point Accuracy Note
For numerical stability when calculating $1 - (1-p)^N$, use precise log-space methods (e.g., math.log1p and math.expm1) to avoid loss of precision when $p$ is close to $0$.
Examples
Example 1
Input: calculate_adoption_metrics(10, 0.2)
Output: [2, 0.8926258176, 0.11111111111111112]
Example 2
Input: calculate_adoption_metrics(1, 0.5)
Output: [0.5, 0.5, 0.3333333333333333]
Example 3
Input: calculate_adoption_metrics(100, 0.01)
Output: [1, 0.6339676587267705, 0.005025125628140704]
Example 4
Input: calculate_adoption_metrics(2, 1)
Output: [2, 1, 1]
Hints
Hint 1
For the expected number of adopters, use linearity of expectation across the N users.
Hint 2
For the probability of at least one adopter out of N, calculate the complement event: 1 - P(none adopt).
Hint 3
For two specific users A and B, P(A and B | A or B) = P(A and B) / P(A or B). Express this in terms of p.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
We can compute each of the three requested metrics using basic probability theory:
-
Expected Adopters: Let $X_i$ be an indicator random variable that equals 1 if user $i$ adopts and 0 otherwise. $E[X_i] = p$. By linearity of expectation: $$E[\sum_{i=1}^N X_i] = \sum_{i=1}^N E[X_i] = N \times p$$
-
Probability of At Least One Adopter: Using the complement rule, the probability of at least one user adopting out of $N$ is $1 - P(\text{no users adopt})$. Since each user independently fails to adopt with probability $(1-p)$, $P(\text{no users adopt}) = (1-p)^N$. Thus, $P(\text{at least one}) = 1 - (1-p)^N$.
To avoid loss of precision due to floating-point underflow when $p$ is small and $N$ is large, we compute $1 - (1-p)^N$ using log-space operations: $$1 - (1-p)^N = 1 - e^{N \ln(1-p)} = -\text{expm1}(N \cdot \text{log1p}(-p))$$
-
Conditional Probability for Two Users: Let $A$ and $B$ be the events that user 1 and user 2 adopt, respectively. We want $P(A \cap B \mid A \cup B)$. $$P(A \cap B \mid A \cup B) = \frac{P((A \cap B) \cap (A \cup B))}{P(A \cup B)} = \frac{P(A \cap B)}{P(A \cup B)}$$ Under independence:
- $P(A \cap B) = p^2$
- $P(A \cup B) = 1 - (1-p)^2 = 2p - p^2 = p(2-p)$
Dividing numerator by denominator yields: $$\frac{p^2}{p(2-p)} = \frac{p}{2-p}$$ For $p = 0.0$, this evaluates to $0.0$.
Complexity
- Time Complexity: $O(1)$ constant time operations using standard mathematical formulas and functions.
- Space Complexity: $O(1)$ auxiliary memory required.
Python Reference Solution
import math
from typing import List
def calculate_adoption_metrics(n: int, p: float) -> List[float]:
expected = n * p
if p == 1.0:
at_least_one = 1.0 if n > 0 else 0.0
elif n == 0:
at_least_one = 0.0
else:
at_least_one = -math.expm1(n * math.log1p(-p))
conditional_both = 0.0 if p == 0.0 else p / (2.0 - p)
return [float(expected), float(at_least_one), float(conditional_both)]