KatInterview

Pooled Reply Rates per Category with Wilson Intervals in pandas

SQL & DataModel answer
MediumModel answerSQL & Datapandasgroupbywilson-interval

Last updated

Clean a job-postings table, compute invitation-weighted reply rates per category with 95% Wilson intervals, rank the top 5, and check the weighting identity numerically.

A marketplace exports a CSV where each row is one job posting:

columnmeaning
job_ididentifier of the posting
job_categorycategory label
invitations_sentinteger >= 0, providers invited to the job
provider_responsesinteger >= 0, providers who replied
regionregion of the job
created_atISO date the job was created

Write pandas code that does the following.

  1. Compute a per-job response_rate = provider_responses / invitations_sent. Rows with invitations_sent = 0 have an undefined rate and must be excluded.
  2. For each job_category, compute the invitation-weighted response rate and a 95% Wilson score confidence interval.
  3. Return the top 5 categories by weighted rate. Break ties using the lower bound of the interval (higher is better).
  4. Handle bad data robustly. Rows with provider_responses > invitations_sent, negative counts, or impossible dates must be logged and dropped, not allowed to crash the pipeline or skew results.
  5. Verify numerically that the job-level invitation-weighted average equals the overall rate computed from summed numerators and denominators, within 1e-9.

Explain your design choices and the complexity of your approach.

Hints

Hint 1

Write out the weighted mean of per-job rates with weights equal to invitations. The invitation factor cancels, so the result is a ratio of two sums. What does that imply about how to aggregate?

Hint 2

Build a dictionary of named boolean masks, one per validation rule. Log the count and a few example job_ids for each rule, OR the masks together, and drop once. Remember that NaN comparisons evaluate to False, so fill them as invalid.

Hint 3

Treat each category as a binomial with n equal to total invitations and successes equal to total responses. Implement the Wilson formula vectorized and clip to [0, 1]. Sort with the rate and lower bound descending and the category name as a final deterministic key.