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
Data Manipulation (SQL/Python)
Software Engineer
Meta
November 29, 2026
Software Engineer
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

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
hashing
experiment assignment
sample ratio mismatch
chi-square
determinism
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
  • 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 Problems
Sample 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...


Submit Your Answer
Markdown supported