Sales Volume Needed to Hold Margin After a Menu Expansion
Math & Bit ManipulationAuto-graded · 17 test casesLast updated
Derive a closed-form sales volume that keeps profit margin unchanged after adding a second product with extra fixed costs, and reject unreachable targets.
A food company sold a single item last year and earned a profit margin (profit divided by revenue) of prior_margin. This year it adds a second item, and you must find how many units it needs to sell in total to end up with exactly the same margin.
All money amounts are in millions of dollars, and unit counts are in millions of units.
Product data (unchanged from last year)
| Item | Selling price | Variable cost per unit |
|---|---|---|
| Classic | $4 | $1 |
| Plant-based | $4 | $2 |
Units sold are split Plant-based : Classic = 2 : 3 (so 40% plant-based and 60% classic).
Fixed costs this year
- Fixed costs that already existed:
base_fixed_costs_mper year. - New staff training: $60 million per year.
- New supplier contract: $2.25 million per month, which is paid for all 12 months.
Let N be the total units sold this year (in millions), following the mix above. Profit is revenue minus variable costs minus all fixed costs (existing plus new). Find the N for which
profit / revenue == prior_margin.
Function behavior
- Return
Nas a float, in millions of units. - If no positive volume can reach the target margin (the target margin is at least the blended contribution margin of the mix), raise a
ValueError.
Constraints
0 <= prior_margin < 10 <= base_fixed_costs_m <= 10^6- Prices, costs, mix and new fixed costs are exactly as given above.
- Return a float. Results are compared numerically, so do not round.
Examples
Example 1
Input: volume_for_same_margin(0.25, 100)
Output: 116.875
Example 2
Input: volume_for_same_margin(0.2, 50)
Output: 76.11111111111111
Example 3
Input: volume_for_same_margin(0.3, 0)
Output: 62.14285714285714
Hints
Hint 1
Blend the two items using the 2:3 mix to get an average variable cost per unit, then the average contribution per unit (price minus that cost).
Hint 2
Write profit = contribution * N - total_fixed and revenue = 4 * N, then set profit / revenue equal to prior_margin and solve the linear equation for N.
Hint 3
Total fixed costs = base + 60 + 12 * 2.25. If the coefficient of N after rearranging is zero or negative, the margin target cannot be reached, so raise ValueError.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
-
Because the mix is 2:3, 40% of units are plant-based and 60% are classic. Both sell at $4. The average variable cost is 0.4 * 2 + 0.6 * 1 = $1.40, so the average contribution per unit is $2.60. This equals (2 * 2 + 3 * 3) / 5.
-
Total fixed costs are the existing amount plus the new ones: 60 + 12 * 2.25 = 87, so
F = base_fixed_costs_m + 87. -
With
Nmillion units, revenue is4Nand profit is2.6N - F. Requiring profit / revenue =m(the prior margin) gives2.6N - F = 4mN, soN * (2.6 - 4m) = F, soN = F / (2.6 - 4m). -
Feasibility: fixed costs can only pull the margin down from the blended contribution margin of 2.6 / 4 = 65%. If
2.6 - 4m <= 0(that is,m >= 65%), no positive volume works, so raiseValueError.
Why it is correct
The margin condition is linear in N once you multiply both sides by revenue. The solution is unique, and it is positive exactly when the denominator is positive, because F > 0. Sample check: for m = 0.25 and a base of 100, N = 187 / 1.6 = 116.875.
Complexity
Time O(1), space O(1).
Reference solution
def volume_for_same_margin(prior_margin: float, base_fixed_costs_m: float) -> float:
# Mix 2 plant-based : 3 classic.
# Average contribution per unit = (2 * (4 - 2) + 3 * (4 - 1)) / 5 = 2.6
# New fixed costs per year = 60 + 12 * 2.25 = 87
# Margin condition: (2.6 * N - F) / (4 * N) = prior_margin
# => N * (2.6 - 4 * prior_margin) = F
denominator = 2.6 - 4 * prior_margin
if denominator <= 0:
raise ValueError("The target margin is unattainable with this product mix")
return (base_fixed_costs_m + 87) / denominator