KatInterview

Audit a Flaky Free-Trial Rollout Function for Experiment Safety

Statistics & ExperimentsModel answer
MediumModel answerStatistics & Experimentsexperimentationidempotencydata-integrity

Last updated

Find at least eight correctness and reliability bugs in a Python function that randomizes users into a free-trial test, then redesign it so the experiment and billing data stay trustworthy.

A growth team is testing a one-month free trial. The function below decides which arm a user belongs to, records that the user was exposed, and calls the billing service to begin the trial for treatment users.

import random, datetime, requests

def maybe_start_trial(user_id):
    if random.random() < 0.5:
        variant = 'trial'
    else:
        variant = 'control'
    now = datetime.datetime.now()
    exposed_at = now
    log_exposure(user_id, exposed_at, variant)
    if variant == 'trial':
        r = requests.post('https://billing/start_trial', json={'user_id': user_id})
        if r.status_code == 200:
            log_trial(user_id, now)

(Assume the indentation was lost when the code was pasted, and that the logic is what matters.)

Your tasks

  1. Identify at least eight distinct problems that could corrupt the experiment or the underlying data. Consider assignment stability, bias, time zones, idempotency, retries, race conditions, privacy and observability.
  2. For each problem, describe the failure mode and its effect on experiment metrics.
  3. Propose concrete fixes. Examples: stable hash-based bucketing with per-experiment salts, UTC timestamps, exactly-once exposure logging, an outbox or queue, idempotency keys, transactional writes, circuit breakers, exponential backoff, and eligibility checks.
  4. Sketch a hardened version (pseudocode or high-level design) that is safe for production use and for experiment analysis.

Hints

Hint 1

Walk the function line by line and ask: what happens if this runs twice for the same user, runs concurrently, or crashes halfway through?

Hint 2

Separate the three responsibilities: deciding the arm, recording exposure, and causing a billing side effect. Each needs a different guarantee (determinism, uniqueness, idempotency).

Hint 3

Think about how each bug would show up in analysis: diluted effect, sample-ratio mismatch, non-compliance in the treatment arm, or misaligned cohort dates.

Hint 4

For the fix, look at the transactional outbox pattern: write assignment, exposure and a pending job atomically, then let a worker call billing with an idempotency key.