Detect a Rate Reversal Between Strata and Pooled Totals
Math & Bit ManipulationAuto-graded · 21 test casesLast updated
Compare two groups' success rates per stratum and pooled, using exact integer cross-multiplication, to detect when a consistent stratum winner flips in the aggregate.
Two groups, A and B, are each split into the same ordered list of strata (subgroups). For every stratum you are given a pair [successes, trials] for each group. Your job is to decide which group has the higher success rate in each stratum and in the pooled data, and to report whether the pooled result reverses a consistent per-stratum result.
Task
Write a function that takes group_a and group_b, two lists of [successes, trials] pairs. The i-th pair of group_a and the i-th pair of group_b describe the same stratum.
Comparing two rates
The success rate of a pair is successes / trials. To compare the rate of A with the rate of B, given (sa, ta) and (sb, tb):
- A wins if
sa * tb > sb * ta. - B wins if
sa * tb < sb * ta. - Otherwise the result is a tie.
Use this exact integer cross-multiplication rule for every comparison.
What to compute
-
Stratum winners: for each stratum in order, the result of comparing A's pair with B's pair. Each entry is the string
"A","B", or"tie". -
Aggregate winner: sum successes and trials across all strata separately for each group, then compare the two pooled pairs with the same rule. The result is
"A","B", or"tie". -
Paradox flag:
trueonly if all of the following hold:- there is at least one stratum;
- every stratum winner is
"A"or"B"(no ties); - all stratum winners are the same group;
- the aggregate winner is
"A"or"B"; - the aggregate winner differs from the common stratum winner.
Otherwise it is
false.
Return value
Return a dictionary with exactly these keys:
"paradox": a boolean"aggregate_winner": a string,"A","B", or"tie""stratum_winners": a list of strings, one per stratum, in input order
Constraints
group_aandgroup_bhave the same length.- Each entry is a two-element list
[successes, trials]of non-negative integers withsuccesses <= trials. - Every group's trial counts are intended to be positive for a meaningful rate, but the cross-multiplication rule above is what defines the result in all cases.
- If the lists are empty,
stratum_winnersis empty andparadoxisfalse. - Use only integer arithmetic; do not use floating-point division.
Examples
Example 1
Input: detect_rate_reversal([[9, 10], [1, 90]], [[8, 10], [0, 10]])
Output: {'paradox': True, 'aggregate_winner': 'B', 'stratum_winners': ['A', 'A']}
Example 2
Input: detect_rate_reversal([[8, 10], [2, 10]], [[7, 10], [1, 10]])
Output: {'paradox': False, 'aggregate_winner': 'A', 'stratum_winners': ['A', 'A']}
Example 3
Input: detect_rate_reversal([[1, 2], [1, 2]], [[1, 2], [1, 2]])
Output: {'paradox': False, 'aggregate_winner': 'tie', 'stratum_winners': ['tie', 'tie']}
Hints
Hint 1
Avoid floating-point division: compare sa/ta with sb/tb by comparing sa*tb with sb*ta.
Hint 2
Write one small helper that takes two (successes, trials) pairs and returns "A", "B" or "tie", then reuse it for every stratum and for the pooled totals.
Hint 3
The flag needs a non-empty list, no ties among strata, a single common stratum winner, and an aggregate winner that is not a tie and differs from it.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Everything reduces to one comparison primitive: given two fractions sa/ta and sb/tb, decide which is larger. Cross-multiplying gives sa*tb versus sb*ta, which avoids floating-point error and any division. Python integers are arbitrary precision, so the comparison is exact.
- Build
stratum_winnersby applying the comparison to each aligned pair of strata. - Sum successes and trials over all strata for each group, then apply the same comparison to get
aggregate_winner. - Set
paradoxto true only when the strata are non-empty, none is a tie, they all agree on one winner, the aggregate is not a tie, and the aggregate winner is the other group.
Why it is correct
The aggregate rate of a group is a weighted average of its stratum rates, with weights equal to the group's own trial counts. When the two groups have different mixes across strata, the weights differ, so the pooled comparison can disagree with every per-stratum comparison. The code checks the definition directly: a unanimous, tie-free stratum result that the pooled result contradicts. Ties in any stratum or in the pooled data are excluded explicitly, matching the rules in the statement.
For example, with A = [[9,10],[1,90]] and B = [[8,10],[0,10]], A wins both strata (0.9 vs 0.8, and 1/90 vs 0). Pooled, A has 10/100 and B has 8/20, so B wins, and the flag is true.
Complexity
- Time: O(n) for n strata.
- Space: O(n) for the list of stratum winners.
Reference solution
from typing import List, Dict
def detect_rate_reversal(group_a: List[List[int]], group_b: List[List[int]]) -> Dict[str, object]:
def winner(success_a, total_a, success_b, total_b):
left = success_a * total_b
right = success_b * total_a
if left > right:
return "A"
if left < right:
return "B"
return "tie"
total_success_a = sum(row[0] for row in group_a)
total_count_a = sum(row[1] for row in group_a)
total_success_b = sum(row[0] for row in group_b)
total_count_b = sum(row[1] for row in group_b)
aggregate_winner = winner(
total_success_a,
total_count_a,
total_success_b,
total_count_b,
)
stratum_winners = [
winner(a[0], a[1], b[0], b[1])
for a, b in zip(group_a, group_b)
]
paradox = (
len(stratum_winners) > 0
and all(w in ("A", "B") for w in stratum_winners)
and len(set(stratum_winners)) == 1
and aggregate_winner in ("A", "B")
and aggregate_winner != stratum_winners[0]
)
return {
"paradox": paradox,
"aggregate_winner": aggregate_winner,
"stratum_winners": stratum_winners,
}