KatInterview

Auditing an A/B Assignment and Free-Trial Trigger Script

Statistics & ExperimentsModel answer
MediumModel answerStatistics & Experimentsexperimentationcode-reviewidempotency

Last updated

Review a script that randomly splits users into experiment arms and sends a free-trial offer, then list its bugs and data risks and propose a safer, reproducible design.

Reviewing an experiment assignment job

A growth team runs a small Python job that does two things for a list of users:

  1. Puts each user into an experiment group, either control or treatment.
  2. Sends a free-trial offer to users placed in treatment.

The script is roughly this:

import random

def assign_and_trigger(users):
    assignments = {}
    for u in users:
        if random.random() < 0.5:
            assignments[u["id"]] = "treatment"
            send_free_trial(u["email"])
        else:
            assignments[u["id"]] = "control"
    return assignments

The job runs on a schedule, and it can also be retried or re-run manually after a failure.

What to do

  • Inspect the script and list the bugs and data-quality risks you can find. Cover randomness, reruns, input data, side effects and analysis.
  • Propose concrete refactors or extra checks that make assignment and triggering safe and reproducible.
  • Sketch what the improved code or design looks like, and explain how you would monitor the experiment once it is live.

If your own version of the script differs, apply the same checklist to it. State any assumptions you make about the data store and the messaging service.

Hints

Hint 1

Ask what happens if the job runs twice, or crashes halfway through the loop. Which users get a second assignment or a second email?

Hint 2

Replace the call to the random number generator with a function of the experiment name, a salt and the user ID, so the result does not depend on run order or global state.

Hint 3

Separate three steps: validate and deduplicate input, persist the assignment with insert-if-absent semantics, then trigger the offer under an idempotency key. Add logs for assignment and exposure, plus a sample-ratio-mismatch check.