Count realized users per variant from salted-hash weighted experiment assignment

Quick Overview

Implement a pandas function that assigns user ids to weighted experiment variants with a salted MD5 bucket and reports how many users landed in each variant. It tests half-open range boundaries, an exact output contract, keeping zero-count variants, and how the counts feed a sample ratio mismatch check.

Count realized users per variant from salted-hash weighted experiment assignment

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `variant_counts(user_ids, salt, weights)` to report the realized allocation of a weighted experiment: how many of the given ids land in each variant. Bucket each id with this expression, used verbatim: ```python bucket = int(hashlib.md5(f"{salt}:{user_id}".encode("utf-8")).hexdigest(), 16) % 100 ``` Walk `weights` in insertion order with a running cumulative total. The id belongs to the first variant whose cumulative total is strictly greater than `bucket`. Requirements: - Raise `ValueError` if the weights do not sum to exactly 100. - Variant i owns the half-open range `[cum_{i-1}, cum_i)`, so a bucket equal to a cumulative total goes to the next variant. - Return a `pd.Series` indexed by variant name in `weights` insertion order (index name `"variant"`), with Series name `"n"` and dtype `int64`. - Variants with zero users still appear with count 0, including when `user_ids` is empty. With `weights = {"a": 20, "b": 30, "c": 50}` the cumulative totals are 20, 50 and 100, so buckets 0-19 go to `a`, 20-49 go to `b` and 50-99 go to `c`. A bucket of exactly 20 belongs to `b`. ```python weights = {"control": 50, "treatment": 50} variant_counts([], salt="exp-42", weights=weights) # variant # control 0 # treatment 0 # Name: n, dtype: int64 variant_counts(["u1"], salt="exp-42", weights={"control": 60, "treatment": 30}) # raises ValueError: the weights sum to 90 ``` ```hint Boundary buckets Trace a bucket that equals one of the cumulative totals. The comparison you choose decides which variant gets it. ``` ```hint Empty variants Counting only the variants that actually received ids will drop some rows. Think about what the output should be built from. ``` ### Constraints & Assumptions - Buckets are integers from 0 to 99, so each weight is a whole number of percentage points. - `weights` maps variant name to an integer weight, and Python dicts keep insertion order. - `user_ids` is a list of strings and may be empty. ### What a Strong Answer Covers - Reproducing the bucketing expression exactly and validating the weight sum before doing any work. - How the strict comparison against cumulative totals implements the half-open ranges. - Keeping every declared variant in insertion order, including zero-count variants and the empty-input case. - Meeting the exact output contract: index name, Series name and `int64` dtype. - The cost of the scan and how it changes with many ids or many variants. ### Follow-up Questions - How would you run a sample ratio mismatch (SRM) chi-square check on this Series? - Why hash the id with a salt instead of assigning each user with a random draw? - If you ramp treatment from 10% to 50% by editing the weights, which users change variant, and when does the order of variants matter? - How would you speed this up for tens of millions of ids?

Overview: Implement a pandas function that assigns user ids to weighted experiment variants with a salted MD5 bucket and reports how many users landed in each variant. It tests half-open range boundaries, an exact output contract, keeping zero-count variants, and how the counts feed a sample ratio mismatch check.

Nov 24, 2026
mediumData ScientistOtherAnalytics & Experimentation
0
0

Implement variant_counts(user_ids, salt, weights) to report the realized allocation of a weighted experiment: how many of the given ids land in each variant.

Bucket each id with this expression, used verbatim:

bucket = int(hashlib.md5(f"{salt}:{user_id}".encode("utf-8")).hexdigest(), 16) % 100

Walk weights in insertion order with a running cumulative total. The id belongs to the first variant whose cumulative total is strictly greater than bucket.

Requirements:

  • Raise ValueError if the weights do not sum to exactly 100.
  • Variant i owns the half-open range [cum_{i-1}, cum_i) , so a bucket equal to a cumulative total goes to the next variant.
  • Return a pd.Series indexed by variant name in weights insertion order (index name "variant" ), with Series name "n" and dtype int64 .
  • Variants with zero users still appear with count 0, including when user_ids is empty.

With weights = {"a": 20, "b": 30, "c": 50} the cumulative totals are 20, 50 and 100, so buckets 0-19 go to a, 20-49 go to b and 50-99 go to c. A bucket of exactly 20 belongs to b.

weights = {"control": 50, "treatment": 50}

variant_counts([], salt="exp-42", weights=weights)
# variant
# control      0
# treatment    0
# Name: n, dtype: int64

variant_counts(["u1"], salt="exp-42", weights={"control": 60, "treatment": 30})
# raises ValueError: the weights sum to 90

Constraints & Assumptions

  • Buckets are integers from 0 to 99, so each weight is a whole number of percentage points.
  • weights maps variant name to an integer weight, and Python dicts keep insertion order.
  • user_ids is a list of strings and may be empty.

What a Strong Answer Covers Guidance

  • Reproducing the bucketing expression exactly and validating the weight sum before doing any work.
  • How the strict comparison against cumulative totals implements the half-open ranges.
  • Keeping every declared variant in insertion order, including zero-count variants and the empty-input case.
  • Meeting the exact output contract: index name, Series name and int64 dtype.
  • The cost of the scan and how it changes with many ids or many variants.

Follow-up Questions Guidance

  • How would you run a sample ratio mismatch (SRM) chi-square check on this Series?
  • Why hash the id with a salt instead of assigning each user with a random draw?
  • If you ramp treatment from 10% to 50% by editing the weights, which users change variant, and when does the order of variants matter?
  • How would you speed this up for tens of millions of ids?