Rake Survey Weights to Match Age and Country Population Margins

Last updated: November 29, 2026

Quick Overview

A Facebook user survey over-represents some ages and countries. Given respondents (respondent_id, age_group, country) and target population shares for each age group and each country, compute raking weights by iterative proportional fitting so weighted margins match both targets, then report the weighted satisfaction score.

Meta
Data Manipulation (SQL/Python)
Data Scientist
Meta
November 29, 2026
Data Scientist
Onsite
Data Manipulation (SQL/Python)
Hard

0

0

0 solved


A Facebook user survey over-represents some ages and countries. Given respondents (respondent_id, age_group, country) and target population shares for each age group and each country, compute raking weights by iterative proportional fitting so weighted margins match both targets, then report the weighted satisfaction score.

Meta research teams run large in-product surveys whose respondents skew toward heavy users and certain markets. This onsite question checks whether a candidate can implement raking with groupby and map, reason about convergence, and understand when weights blow up and need trimming.

What the Interviewer Expects
  • Explain raking as alternating adjustments to each margin
  • Implement each step with groupby sums and map, vectorized
  • Stop on a convergence tolerance with a max iteration guard
  • Normalize weights to mean 1 and report the design effect
  • Discuss trimming extreme weights and empty cells
Key Topics to Cover
raking
iterative proportional fitting
survey weighting
groupby map
design effect
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • Why not just weight on the full age by country cross-classification (post-stratification)?
  • How would you compute a standard error for the weighted mean?
  • What happens if a target category has no respondents?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

Targets are dicts of shares that each sum to 1, for example age {'18-24': 0.3, '25-44': 0.5, '45+': 0.2} and country {'US': 0.4, 'IN': 0.6}. Every target category must appear among respondents; otherw...

Approach

Start with w = 1. In each iteration, for each dimension compute the current weighted share of each category, p^c=∑i∈cwi/∑iwi\hat p_c = \sum_{i \in c} w_i / \sum_i w_i, and multiply every respondent's weight by $p_...


Submit Your Answer
Markdown supported