KatInterview

Compute Multi-User Feature Adoption Metrics

Math & Bit ManipulationAuto-graded · 20 test cases
EasyAuto-gradedMath & Bit Manipulationmathprobabilitystatistics

Last 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:

  1. Expected number of adopters: The expected count of users who opt into the feature across all $N$ users.
  2. 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$).
  3. 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.