KatInterview

Middle Age Band Share Among Cycling Respondents

Math & Bit ManipulationAuto-graded · 17 test cases
EasyAuto-gradedMath & Bit Manipulationprobabilityfractionstables

Last 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 band i whose main exercise is type j.

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

  • counts has 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.