KatInterview

Group Size Threshold and Closest-Pair Gap on a Circle

Math & Bit ManipulationAuto-graded · 27 test cases
MediumAuto-gradedMath & Bit Manipulationprobabilitymathbirthday-paradox

Last updated

Compute a birthday-collision threshold exactly and by approximation, plus expected minimum gap and tail probability for random points on a circle, with a Poisson comparison.

Collisions and closest gaps

Implement a function that returns five numeric quantities from two related probability models.

Inputs

  • people (int): the number of random points used in the circle model (parts 3 to 5 below).
  • dist (float): a distance threshold for the circle model.
  • days (int, default 365): the number of equally likely calendar days in the discrete birthday model.

Model A: discrete birthdays (uses only days)

Each person's birthday is uniform over days days, independently. Let the collision probability for a group of size k be the probability that at least two people share a birthday.

  1. minimum_people: the smallest group size k whose exact collision probability is strictly greater than 0.5. Build it by multiplying the no-collision factors one person at a time: person k contributes the factor (days - k + 1) / days. If group size exceeds days, a collision is certain (probability 1), so the search can never go past days + 1. Return the value as a float.
  2. approximate_minimum_people: the closed-form estimate sqrt(2 * days * ln 2), as a float (not rounded).

Model B: random points on a circle (uses people and dist)

Place people points independently and uniformly on a circle of circumference 1. They split the circle into exactly people arcs, and the arc lengths are uniformly distributed over all non-negative tuples summing to 1. Let X be the smallest of these arcs (the distance between the closest pair).

  1. expected_minimum_spacing: E[X] = 1 / people^2 when people > 0; return 0.0 when people <= 0.
  2. probability_closest_distance_greater_than_d: the exact value of P(X > dist), using these rules in order:
    • if people <= 0 or dist < 0: 0.0
    • else if dist == 0: 1.0
    • else if people * dist >= 1: 0.0
    • otherwise: (1 - people * dist) ** (people - 1)
  3. poisson_approximation: the Poisson-style estimate exp(-people^2 * dist) when people > 0 and dist >= 0; otherwise 0.0.

Output

Return a dictionary with exactly these keys, all with float values:

  • "minimum_people"
  • "approximate_minimum_people"
  • "expected_minimum_spacing"
  • "probability_closest_distance_greater_than_d"
  • "poisson_approximation"

Constraints

  • days is a non-negative integer. Typical values are small to moderate, so an O(days) loop is fine.
  • people is an integer and may be small or zero in edge cases. Handle it as specified above.
  • dist is a float and may be zero or negative in edge cases.
  • Model A does not depend on people or dist. Model B does not depend on days.
  • Use ordinary double-precision floating point. Compare the collision probability to 0.5 as 1 - no_collision_product > 0.5.

Examples

Example 1

Input: birthday_threshold_and_gap_stats(300, 0.000001, 365)

Output: {'minimum_people': 23, 'approximate_minimum_people': 22.49438689559598, 'expected_minimum_spacing': 0.000011111111111111112, 'probability_closest_distance_greater_than_d': 0.9141931027462054, 'poisson_approximation': 0.9139311852712282}

Example 2

Input: birthday_threshold_and_gap_stats(300, 0.001, 365)

Output: {'minimum_people': 23, 'approximate_minimum_people': 22.49438689559598, 'expected_minimum_spacing': 0.000011111111111111112, 'probability_closest_distance_greater_than_d': 4.83408145724978e-47, 'poisson_approximation': 8.194012623990515e-40}

Example 3

Input: birthday_threshold_and_gap_stats(2, 0.1, 365)

Output: {'minimum_people': 23, 'approximate_minimum_people': 22.49438689559598, 'expected_minimum_spacing': 0.25, 'probability_closest_distance_greater_than_d': 0.8, 'poisson_approximation': 0.6703200460356393}

Hints

Hint 1

For the discrete model, keep a running product of the no-collision factors and stop at the first group size where one minus the product exceeds one half. Remember the pigeonhole cap at days + 1.

Hint 2

For the circle, the arcs are exchangeable. Requiring all of them to exceed dist amounts to shrinking the total length from 1 to 1 - people * dist and raising the ratio to the power people - 1.

Hint 3

The remaining outputs are direct formulas: sqrt(2 * days * ln 2), 1 / people^2, and exp(-people^2 * dist). Apply the guard rules for non-positive people and negative dist in the stated order.