Middle Age Band Share Among Cycling Respondents
Math & Bit ManipulationAuto-graded · 17 test casesLast updated
Restrict a 3x3 survey table to one column and return an exact reduced fraction for a conditional probability.
Survey Table Conditional Share
A wellness app polled a group of people about their main form of exercise and their age band. The answers are stored in a 3x3 grid of counts.
- Each row is an age band, in this order:
18-30,31-50,51+. - Each column is an exercise type, in this order:
Running,Biking,Swimming. counts[i][j]is the number of respondents in age bandiwhose main exercise is typej.
One respondent is picked uniformly at random from everyone in the table. You are told that this person's main exercise is Biking. Compute the probability that the person is in the 31-50 age band.
Return the probability as an exact fraction in lowest terms, formatted as a string "p/q". If the reduced fraction has denominator 1 (for example, the value is 0 or 1), return just the integer as a string, such as "0" or "1".
Use only the numbers in the grid. Do not use floating-point arithmetic for the result.
Constraints
countshas exactly 3 rows and 3 columns.- Every entry is a non-negative integer.
- The Biking column (index 1) has a positive sum.
- The result must be fully reduced (numerator and denominator share no common factor greater than 1).
Examples
Example 1
Input: middle_age_given_biking([[120, 85, 45], [150, 110, 90], [130, 73, 197]])
Output: '55/134'
Hints
Hint 1
Conditioning on Biking means only the Biking column matters; everything else is outside the sample space.
Hint 2
The probability is the 31-50 Biking cell divided by the sum of the whole Biking column. The overall population size cancels out.
Hint 3
Use math.gcd or fractions.Fraction to reduce the ratio, and convert it to a string at the end.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
By definition, P(A | B) = P(A and B) / P(B). Here A is "age 31-50" and B is "Biking". Both probabilities are counts divided by the same total population, so that total cancels:
P(A | B) = (number of people in 31-50 who bike) / (number of people who bike)
The numerator is the single cell counts[1][1]. The denominator is the sum of the Biking column, counts[0][1] + counts[1][1] + counts[2][1].
Python's Fraction reduces automatically by dividing out the greatest common divisor. Its string form is "p/q", or just "p" when the denominator is 1, which matches the required output format.
Why it is correct
Given that the person bikes, the sample space shrinks to the Biking column, and each person in it is equally likely to have been chosen. The favourable outcomes are those in that column who are also in the 31-50 band. Dividing favourable by total in the restricted space gives the conditional probability. Using exact integer arithmetic means there are no rounding errors.
For the sample table, the Biking column sums to 85 + 110 + 73 = 268 and the favourable count is 110, so the result is 110/268 = 55/134.
Complexity
- Time: O(1), since the table is fixed at 3x3 (the gcd step is logarithmic in the values).
- Space: O(1).
Reference solution
from fractions import Fraction
from typing import List
def middle_age_given_biking(counts: List[List[int]]) -> str:
biking_total = sum(row[1] for row in counts)
return str(Fraction(counts[1][1], biking_total))