Streak Follow-Up Chance and Two-Bot Proportion Comparison
Arrays & HashingAuto-graded · 21 test casesLast 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.
- Let
p_a = good_a / total_aandp_b = good_b / total_b. - Let the pooled rate be
p = (good_a + good_b) / (total_a + total_b). - Let the standard error be
se = sqrt(p * (1 - p) * (1 / total_a + 1 / total_b)). - 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. - The two-sided p-value is
2 * (1 - Phi(|z|)), wherePhiis the standard normal CDF. Equivalently, it iserfc(|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 <= 1total_a >= 1andtotal_b >= 10 <= good_a <= total_aand0 <= 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Part 1. Because replies are independent, each has probability x of being good no matter what came before:
P(4th good | first 3 good) = P(all 4 good) / P(first 3 good) = x^4 / x^3 = x.
The answer is just x. Believing a streak makes the next reply more or less likely is the gambler's fallacy.
Part 2. Under H0 both assistants share one true rate, so the best estimate of that rate uses all the data (the pooled proportion). From it we get the standard error of the difference in sample proportions under H0:
se = sqrt(p(1 - p)(1/n_A + 1/n_B)).
Dividing the observed difference p_a - p_b by se gives a z statistic that is approximately standard normal under H0. The two-sided p-value is the probability of a value at least as extreme in either tail, which is erfc(|z| / sqrt(2)).
Why it is correct
- The conditional probability collapses to
xby the independence assumption. - The pooled standard error is the right one for a test of equal rates, since the variance is evaluated under the null.
erfc(|z|/sqrt(2)) = 2 * (1 - Phi(|z|)), so it matches the definition of the two-sided p-value.- If
se == 0, the pooled rate is 0 or 1, which means both groups have the same rate (all bad or all good). The statement's special case covers this, givingz = 0andp = 1. Theinfbranch is kept only for safety.
Complexity
Time O(1), space O(1).
Reference solution
import math
from typing import List
def streak_and_proportion_test(x: float, good_a: int, total_a: int, good_b: int, total_b: int) -> List[float]:
# Part 1: independence means earlier replies do not change the next one.
next_good = x
# Part 2: pooled two-proportion z-test.
p_a = good_a / total_a
p_b = good_b / total_b
pooled = (good_a + good_b) / (total_a + total_b)
se = math.sqrt(pooled * (1 - pooled) * (1 / total_a + 1 / total_b))
if se == 0:
z = 0.0 if p_a == p_b else float('inf')
p_value = 1.0 if p_a == p_b else 0.0
else:
z = (p_a - p_b) / se
p_value = math.erfc(abs(z) / math.sqrt(2))
return [next_good, z, p_value]