方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Implement A/B testing for model versions

Implement A/B Testing for Model Versions

When you want to test a new model version or prompt in production, rolling it out to all users at once is risky. A/B testing lets you route a fraction of traffic to the candidate version, collect quality metrics, and compare against the control before committing to a full rollout.

What to build

Implement an ABTestRouter that:

  1. Takes a control_config and candidate_config (each with version_id: str).
  2. assign(user_id: str, test_id: str) -> str — deterministically returns either "control" or "candidate". The same user always gets the same assignment for the same test (sticky assignment via hash).
  3. record_outcome(test_id: str, variant: str, metric_name: str, value: float) -> None — record a quality metric for a variant.
  4. get_results(test_id: str) -> ABTestResults — return aggregated results for both variants.
  5. ABTestResults has per-variant n and mean for each recorded metric.
  6. should_promote(test_id: str, metric: str, min_improvement_pct: float = 5.0) -> bool — return True if candidate outperforms control by at least min_improvement_pct percent.

Constraints

  • Standard library only.
  • Candidate percentage is configurable (default 10% of traffic).
  • Sticky assignment must be deterministic: same user_id + test_id always returns same variant.

Evaluation / medium / Step 14 of 36

Practice stage

Evaluation and review loops

Hint

Separate the scoring logic from the interpretation logic. Your goal is not just a number; it is a useful next action.

Success criteria
  • - Produces a useful signal, not decorative output
  • - Makes regression review easier
  • - Would support a benchmark or observability loop
Review checklist
  • - Would this output help decide what to fix next?
  • - Are important failure modes visible?
  • - Does the score hide any ambiguity I should record?

Practice

Generate a variation

Generate a new exercise variation to deepen understanding or practice a related concept.

Attempt history

Recent submissions

Before you submit, decide what a strong answer should make obvious to the reviewer.

No attempts yet.