KatInterview

Streak Follow-Up Chance and Two-Bot Proportion Comparison

Arrays & HashingAuto-graded · 21 test cases
MediumAuto-gradedArrays & Hashinghypothesis-testingtwo-proportion-z-testconditional-probability

Last updated

Return the next-success probability under independence, plus a pooled two-proportion z statistic and two-sided p-value comparing two success rates.

Streak Follow-Up Chance and Two-Bot Proportion Comparison

You are evaluating two chat assistants, A and B. Write one function that answers two questions and returns the results together.

Part 1: Next response after a streak

An assistant gives a good reply with a fixed, known probability x. Each reply is independent of all the others. Given that its first three replies were all good, what is the probability that the fourth reply is also good?

Part 2: Comparing the two assistants

Assistant A had good_a good replies out of total_a graded replies. Assistant B had good_b good replies out of total_b.

Run a pooled two-proportion z-test of H0: the two true good-reply rates are equal, against the two-sided alternative.

  1. Let p_a = good_a / total_a and p_b = good_b / total_b.
  2. Let the pooled rate be p = (good_a + good_b) / (total_a + total_b).
  3. Let the standard error be se = sqrt(p * (1 - p) * (1 / total_a + 1 / total_b)).
  4. The test statistic is z = (p_a - p_b) / se. Note the order: A minus B, so the sign is negative when B has the higher rate.
  5. The two-sided p-value is 2 * (1 - Phi(|z|)), where Phi is the standard normal CDF. Equivalently, it is erfc(|z| / sqrt(2)).

Degenerate case. If se is exactly 0, use z = 0.0 and p_value = 1.0 when p_a == p_b. Otherwise use z = inf and p_value = 0.0.

Task

Return a list of three floats: [probability_from_part_1, z, p_value].

Constraints

  • 0 <= x <= 1
  • total_a >= 1 and total_b >= 1
  • 0 <= good_a <= total_a and 0 <= good_b <= total_b
  • Use only the Python standard library (math).
  • Return the values as plain floats, in the order shown above.

Examples

Example 1

Input: streak_and_proportion_test(0.7, 70, 100, 80, 100)

Output: [0.7, -1.6329931618554536, 0.10247043485974913]

Example 2

Input: streak_and_proportion_test(0.4, 140, 200, 160, 200)

Output: [0.4, -2.309401076758505, 0.020921335337793924]

Example 3

Input: streak_and_proportion_test(0.9, 45, 50, 40, 50)

Output: [0.9, 1.4002800840280094, 0.16142946236708333]

Hints

Hint 1

For Part 1, write the conditional probability as P(all four good) / P(first three good) and simplify using independence.

Hint 2

The pooled proportion is used for the standard error because the test assumes the null hypothesis, where both rates are the same. Compute z as (p_a - p_b) / se so the sign matches.

Hint 3

You do not need scipy. A two-sided normal p-value equals math.erfc(abs(z) / math.sqrt(2)). Handle se == 0 separately to avoid division by zero.