KatInterview

Tight Probability Bounds for the Union of Two Events

Arrays & HashingAuto-graded · 23 test cases
EasyAuto-gradedArrays & Hashingprobabilityboundsinclusion-exclusion

Last updated

Tests whether a candidate can derive the Fréchet bounds on P(A ∪ B) given only the marginal probabilities of two events with unspecified dependence.

You are given two events $A$ and $B$ with known marginal probabilities $P(A) = p$ and $P(B) = q$. The relationship between the two events — whether they are independent, positively correlated, or negatively correlated — is completely unspecified.

Your task is to find the tightest possible range for $P(A \cup B)$: the smallest value it could take and the largest value it could take, over all valid joint distributions that are consistent with the two marginals.

Return both endpoints as a list [lower, upper].

A bound is considered tight only if there exists a real joint distribution that actually achieves it. Loose or approximate bounds are not accepted.

Background

By inclusion–exclusion:

$$P(A \cup B) = p + q - P(A \cap B)$$

The marginals $p$ and $q$ are fixed. The only free quantity is the overlap $P(A \cap B)$. Finding the range of $P(A \cup B)$ reduces to finding the range of the overlap, because the two are linearly related (and in opposite directions).

What to Return

Return a list of two floats [lower_bound, upper_bound] where:

  • lower_bound is the smallest possible value of $P(A \cup B)$
  • upper_bound is the largest possible value of $P(A \cup B)$

Both values must be tight: each must be achievable by some valid joint distribution with marginals $p$ and $q$.

Constraints

  • $0.0 \le p \le 1.0$
  • $0.0 \le q \le 1.0$
  • $p$ and $q$ are given as floats
  • No assumption is made about the dependence structure between $A$ and $B$
  • The returned bounds must be exact (no approximation)
  • Return type is List[float] with exactly two elements: [lower, upper]

Examples

Example 1

Input: union_probability_bounds(0.4, 0.3)

Output: [0.4, 0.7]

Example 2

Input: union_probability_bounds(0.7, 0.6)

Output: [0.7, 1]

Example 3

Input: union_probability_bounds(0.2, 0.2)

Output: [0.2, 0.4]

Hints

Hint 1

Use inclusion–exclusion: P(A ∪ B) = p + q − P(A ∩ B). The union is fully determined by the overlap, so bounding the union is equivalent to bounding P(A ∩ B).

Hint 2

For the overlap, think about what constraints probability axioms impose: the overlap cannot be negative, cannot exceed either marginal, and P(A ∪ B) cannot exceed 1. Each of those constraints pins one endpoint of the overlap's range.

Hint 3

The lower bound on the union has an intuitive reading: the union must contain each individual event, so it is at least as likely as the more probable of the two. The upper bound is the union bound, capped at 1.