Implement two-step hash-based experiment assignment with an exposure gate and a variant split

Quick Overview

Implement deterministic A/B test assignment in Python: an MD5 exposure gate admits a share of users, then a second, independent MD5 hash splits admitted users into control and treatment. It tests salted hashing, threshold boundaries, rounding behavior and how assignment design relates to sample ratio mismatch.

Implement two-step hash-based experiment assignment with an exposure gate and a variant split

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `def two_step_assign(user_id: str, salt: str, exposure_rate: float, control_pct: int) -> str`. An exposure gate decides who enters the experiment, then a second, independent `md5` hash buckets the admitted users. Return exactly one of `"ignore"`, `"control"` or `"treatment"`. - Step 1: build `Se = f"{salt}:{user_id}:Exposure"` and compute `Re = int(hashlib.md5(Se.encode("utf-8")).hexdigest(), 16) % 100`. If `Re >= round(exposure_rate * 100)`, return `"ignore"`. - Step 2: build `Sb = f"{salt}:{user_id}:Bucket"` and compute `Rb = int(hashlib.md5(Sb.encode("utf-8")).hexdigest(), 16) % 100`. Return `"control"` if `Rb < control_pct`, else `"treatment"`. - `control_pct` applies to admitted traffic only. For `two_step_assign("u123", "exp_A", 0.5, 50)`, the user is ignored when `Re >= 50`; otherwise the user goes to control when `Rb < 50` and to treatment when it is not. Some settings fix the answer whatever the hash values are: ```text two_step_assign("u123", "exp_A", 0.0, 50) -> "ignore" two_step_assign("u123", "exp_A", 1.0, 100) -> "control" two_step_assign("u123", "exp_A", 1.0, 0) -> "treatment" ``` With an exposure rate of 0 the gate threshold is 0, so every `Re` fails it. With a rate of 1 every `Re` (0 to 99) passes, and then `control_pct` of 100 or 0 sends every admitted user to one arm. ```hint Exact strings The hash input must match the given format character for character, including the colons and the capitalized label. ``` ```hint Boundaries Check each comparison at the extremes: an exposure rate of 0 or 1, a `control_pct` of 0 or 100, and a rate whose value times 100 ends in .5. ``` ### Constraints & Assumptions - `exposure_rate` is a float in [0, 1]. - `control_pct` is an int in [0, 100]. - Assignment must be deterministic per `user_id` and use the exact salt and label formats above. ### What a Strong Answer Covers - Both hash keys built exactly as specified, encoded as UTF-8, with the hex digest parsed in base 16 and reduced with `% 100`. - The gate threshold computed with `round`, and the bucketing applied only to users who pass the gate. - Why the two steps need different hash inputs, and what the expected share of all traffic is for each of the three outputs. - Behavior at the boundary settings, including rates that Python's `round` sends to the even neighbor. ### Follow-up Questions - What can go wrong with this two-step design in practice, and how would you change it? - How would you detect a sample ratio mismatch in the assignments it produces? - Why hash a salted string instead of bucketing on the user ID directly, and why should each experiment get its own salt?

Overview: Implement deterministic A/B test assignment in Python: an MD5 exposure gate admits a share of users, then a second, independent MD5 hash splits admitted users into control and treatment. It tests salted hashing, threshold boundaries, rounding behavior and how assignment design relates to sample ratio mismatch.

General logo
General
Nov 25, 2026
mediumData ScientistOtherAnalytics & Experimentation
0
0

Implement def two_step_assign(user_id: str, salt: str, exposure_rate: float, control_pct: int) -> str. An exposure gate decides who enters the experiment, then a second, independent md5 hash buckets the admitted users. Return exactly one of "ignore", "control" or "treatment".

  • Step 1: build Se = f"{salt}:{user_id}:Exposure" and compute Re = int(hashlib.md5(Se.encode("utf-8")).hexdigest(), 16) % 100 . If Re >= round(exposure_rate * 100) , return "ignore" .
  • Step 2: build Sb = f"{salt}:{user_id}:Bucket" and compute Rb = int(hashlib.md5(Sb.encode("utf-8")).hexdigest(), 16) % 100 . Return "control" if Rb < control_pct , else "treatment" .
  • control_pct applies to admitted traffic only.

For two_step_assign("u123", "exp_A", 0.5, 50), the user is ignored when Re >= 50; otherwise the user goes to control when Rb < 50 and to treatment when it is not. Some settings fix the answer whatever the hash values are:

two_step_assign("u123", "exp_A", 0.0, 50)   -> "ignore"
two_step_assign("u123", "exp_A", 1.0, 100)  -> "control"
two_step_assign("u123", "exp_A", 1.0, 0)    -> "treatment"

With an exposure rate of 0 the gate threshold is 0, so every Re fails it. With a rate of 1 every Re (0 to 99) passes, and then control_pct of 100 or 0 sends every admitted user to one arm.

Constraints & Assumptions

  • exposure_rate is a float in [0, 1].
  • control_pct is an int in [0, 100].
  • Assignment must be deterministic per user_id and use the exact salt and label formats above.

What a Strong Answer Covers Guidance

  • Both hash keys built exactly as specified, encoded as UTF-8, with the hex digest parsed in base 16 and reduced with % 100 .
  • The gate threshold computed with round , and the bucketing applied only to users who pass the gate.
  • Why the two steps need different hash inputs, and what the expected share of all traffic is for each of the three outputs.
  • Behavior at the boundary settings, including rates that Python's round sends to the even neighbor.

Follow-up Questions Guidance

  • What can go wrong with this two-step design in practice, and how would you change it?
  • How would you detect a sample ratio mismatch in the assignments it produces?
  • Why hash a salted string instead of bucketing on the user ID directly, and why should each experiment get its own salt?