Deterministic Hash Bucketing of Users into Experiment Arms
Last updated: November 29, 2026
Quick Overview
Implement experiment assignment in pandas: hash each user_id with an experiment-specific salt into 1,000 buckets, map buckets to arms given an allocation like {'control': 0.5, 'treatment': 0.5}, and run a sample ratio mismatch check on the result.
Meta
November 29, 20260
0
0 solved
Implement experiment assignment in pandas: hash each user_id with an experiment-specific salt into 1,000 buckets, map buckets to arms given an allocation like {'control': 0.5, 'treatment': 0.5}, and run a sample ratio mismatch check on the result.
Meta runs thousands of concurrent experiments, and assignment must be deterministic, independent across experiments and balanced. This screen question checks stable hashing (not Python's randomized hash), mapping buckets to cumulative allocation ranges, and a chi-square SRM check computed by hand.
What the Interviewer Expects
- Use a stable hash such as md5 rather than Python's built-in hash
- Salt with the experiment name so assignments are independent across experiments
- Map buckets to arms via cumulative allocation boundaries
- Run a chi-square goodness-of-fit test on observed arm counts
- Explain why changing allocation later can reshuffle users
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you ramp treatment from 1 percent to 50 percent without moving existing treatment users back to control?
- How would you run mutually exclusive experiments in the same layer?
- What causes SRM in practice even when hashing is correct?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
Input: a frame of user_id, an experiment name, and an allocation dict whose shares sum to 1. Buckets 0 to 999. With 50/50, buckets 0 to 499 are control. The SRM check compares observed counts to exp...
Approach
For each user compute md5(f'{salt}:{user_id}'), take the first 8 hex digits as an integer, modulo 1000. Python's hash() is randomized per process for strings, so it cannot be used. Cumulative allo...