KatInterview

Detect a Rate Reversal Between Strata and Pooled Totals

Math & Bit ManipulationAuto-graded · 21 test cases
EasyAuto-gradedMath & Bit Manipulationsimulationfractionsaggregation

Last 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

  1. 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".

  2. 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".

  3. Paradox flag: true only 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_a and group_b have the same length.
  • Each entry is a two-element list [successes, trials] of non-negative integers with successes <= 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_winners is empty and paradox is false.
  • 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.