Calculate how many whole days an A/B test must run to reach its sample size
Quick Overview
Write a Python function that turns a per-group sample size, daily traffic and an exposure fraction into the number of whole days an A/B test must run. It tests basic experiment planning arithmetic, the handling of partial days, and inputs where the formula can break.
Calculate how many whole days an A/B test must run to reach its sample size
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
Implement `def experiment_days(n_per_group: int, daily_visitors: int, exposure_fraction: float) -> int`. The test needs `n_per_group` units in each of 2 equal groups, and only `exposure_fraction` (0 to 1) of daily visitors enter. Return the number of whole days required.
The function must follow these rules:
- `total_needed = 2 * n_per_group`
- `daily_eligible = daily_visitors * exposure_fraction`, not `daily_visitors` alone
- Return `math.ceil(total_needed / daily_eligible)` as a Python `int`, with no rounding or flooring.
Examples:
```python
experiment_days(3835, 5000, 0.5) # -> 4
experiment_days(2500, 5000, 0.5) # -> 2
```
```hint Partial days
Work out how many units actually enter per day, then decide what a leftover fraction of a day means for a test that has not reached its target yet.
```
### Constraints & Assumptions
- `n_per_group` and `daily_visitors` are non-negative integers, and `exposure_fraction` is a float between 0 and 1.
- There are no repeat visitors and no ramp-up: intake is the same every day.
### What a Strong Answer Covers
- Counting the units needed in both groups.
- Applying the exposure fraction to daily traffic.
- The direction of rounding, with a reason, and the integer return type.
- Inputs that break the formula, such as zero exposure or floating-point error near a whole number.
### Follow-up Questions
- If the unit is a user and many users visit on several days, how does the estimate change?
- Why might a team run this test for a full week even though the formula says 4 days?
- How does the duration change if traffic is split 90/10 instead of 50/50?
Overview: Write a Python function that turns a per-group sample size, daily traffic and an exposure fraction into the number of whole days an A/B test must run. It tests basic experiment planning arithmetic, the handling of partial days, and inputs where the formula can break.
Implement def experiment_days(n_per_group: int, daily_visitors: int, exposure_fraction: float) -> int. The test needs n_per_group units in each of 2 equal groups, and only exposure_fraction (0 to 1) of daily visitors enter. Return the number of whole days required.
The function must follow these rules:
total_needed = 2 * n_per_group
daily_eligible = daily_visitors * exposure_fraction
, not
daily_visitors
alone
Return
math.ceil(total_needed / daily_eligible)
as a Python
int
, with no rounding or flooring.