MediumEvaluationPython 3

Experiment Traffic Splitter

Assign stable experiment variants using weighted deterministic buckets.

30m2 sample tests3 hidden tests

Assign users to weighted experiment variants with stable deterministic bucketing.

Requirements

  • Define assign_variant(user_id, experiment_key, variants).
  • variants is a list of (name, weight) tuples.
  • Ignore variants with weight 0.
  • Use a stable bucket derived from experiment_key and user_id.
  • Return the selected variant name.
  • Return None if total positive weight is 0.
  • Changing experiment_key can change assignment.
  • Calling the function repeatedly with the same inputs must return the same variant.

Example

python
1assert assign_variant("u1", "exp", [("control", 50), ("treatment", 50)]) in {"control", "treatment"}

Constraints

  • Do not use Python's built-in hash, because it is process-randomized.
  • Do not use randomness.
  • Preserve variant order for bucket ranges.

Editor
Results
Run sample tests or submit all tests.