KatInterview

Chance That Two Random Draws Multiply to More Than a Half

Math & Bit ManipulationAuto-graded · 18 test cases
MediumAuto-gradedMath & Bit Manipulationprobabilityintegrationcontinuous-distributions

Last updated

Compute the exact probability that the product of two independent Uniform(0,1) values exceeds 1/2, by integrating over the region of the unit square above a hyperbola.

Two numbers U and V are drawn independently, each uniformly at random from the open interval (0, 1). Consider the event that their product is larger than one half, i.e. U * V > 1/2.

Write a function that returns the probability of this event as a floating-point number.

The function takes no arguments. Your result should be the exact value of the probability, evaluated in floating point, and not an estimate obtained from random simulation.

Notes:

  • The pair (U, V) is uniformly distributed over the unit square, so the probability equals the area of the part of the square where U * V > 1/2.
  • Whether the inequality is strict or not makes no difference, because the boundary curve U * V = 1/2 has zero area.

Constraints

  • U and V are independent and continuously uniform on (0, 1).
  • The function has no parameters and returns a single float.
  • Return the closed-form value computed with standard floating-point math (no Monte Carlo sampling).
  • Answers are compared to the expected value with a tiny numerical tolerance.

Examples

Example 1

Input: prob_product_exceeds_half()

Output: 0.15342640972002736

Example 2

Input: prob_product_exceeds_half()

Output: 0.15342640972002736

Hints

Hint 1

Because the pair is uniform on the unit square, the probability is just an area. Describe the region where u * v > 1/2.

Hint 2

For a fixed u, the condition becomes v > 1/(2u). When is this threshold below 1? When u <= 1/2 no valid v exists, so only u in (1/2, 1) contributes.

Hint 3

For u in (1/2, 1) the valid v-range has length 1 - 1/(2u). Integrate this over u from 1/2 to 1 and use the antiderivative of 1/u, which is ln u.