Deterministic A/B Variant Assignment with Hash Bucketing

Quick Overview

Implement a function that assigns users to control or treatment in a 50/50 A/B test by hashing a salted user id into 100 buckets. It tests reproducible assignment without randomness, getting the bucket boundary right, and what the salt does across experiments.

Deterministic A/B Variant Assignment with Hash Bucketing

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: easy

Interview Round: Other

Implement `def assign_ab(user_id: str, salt: str) -> str` that deterministically assigns a user to one of two variants in a 50/50 A/B test via hash bucketing. Same inputs must always yield the same variant. Requirements: - `bucket = int(hashlib.md5(f"{salt}:{user_id}".encode("utf-8")).hexdigest(), 16) % 100` (salt first, single colon, full digest). - Return `"control"` if `bucket < 50`, else `"treatment"` (bucket 49 → control, 50 → treatment). - No randomness: fully reproducible across processes and machines. - Return a plain Python `str` (not `numpy.str_`). ```hint Count the buckets List which bucket values go to each variant under your comparison. Each side should get exactly 50 of the 100 values. ``` ### What a Strong Answer Covers - Builds the hash key in the exact required format and parses the full digest before taking the bucket. - Splits at the right boundary so buckets 0-49 are control and 50-99 are treatment. - Explains why the assignment is reproducible across processes and machines, and why Python's built-in `hash()` would not be. - Explains what the salt does for assignments across different experiments. ### Follow-up Questions - How would you support an uneven split, e.g. 70/30? - What goes wrong if two experiments running at the same time share the same salt? - After launch, how would you check that the observed split really is close to 50/50?

Overview: Implement a function that assigns users to control or treatment in a 50/50 A/B test by hashing a salted user id into 100 buckets. It tests reproducible assignment without randomness, getting the bucket boundary right, and what the salt does across experiments.

Nov 24, 2026
easyData ScientistOtherAnalytics & Experimentation
0
0

Implement def assign_ab(user_id: str, salt: str) -> str that deterministically assigns a user to one of two variants in a 50/50 A/B test via hash bucketing. Same inputs must always yield the same variant.

Requirements:

  • bucket = int(hashlib.md5(f"{salt}:{user_id}".encode("utf-8")).hexdigest(), 16) % 100 (salt first, single colon, full digest).
  • Return "control" if bucket < 50 , else "treatment" (bucket 49 → control, 50 → treatment).
  • No randomness: fully reproducible across processes and machines.
  • Return a plain Python str (not numpy.str_ ).

What a Strong Answer Covers Guidance

  • Builds the hash key in the exact required format and parses the full digest before taking the bucket.
  • Splits at the right boundary so buckets 0-49 are control and 50-99 are treatment.
  • Explains why the assignment is reproducible across processes and machines, and why Python's built-in hash() would not be.
  • Explains what the salt does for assignments across different experiments.

Follow-up Questions Guidance

  • How would you support an uneven split, e.g. 70/30?
  • What goes wrong if two experiments running at the same time share the same salt?
  • After launch, how would you check that the observed split really is close to 50/50?