Calculate Product Mix Profitability Metrics
Math & Bit ManipulationAuto-graded · 16 test casesLast updated
Computes baseline and mix-shifted unit profit averages, per-unit margin delta, and overall impact on profit given total volume.
Consider two operating scenarios for selling burgers:
- Scenario A: Only Regular burgers are sold.
- Scenario B: Both Regular and Vegan burgers are sold, with a sales ratio of Vegan to Regular equal to $2:3$, and total unit sales remaining unchanged.
The pricing and unit cost structure for each burger type is as follows:
- Regular: Price = $4, Unit Cost = $1.
- Vegan: Price = $4, Unit Cost = $2.
Given total_burgers, representing the total number of units sold, compute the following metrics in order:
- Average profit per burger in Scenario A.
- Average profit per burger in Scenario B.
- Difference in average profit per burger between Scenario B and Scenario A ($\text{Scenario B} - \text{Scenario A}$).
- Total profit change across all
total_burgersunits ($\text{Difference} \times \text{total_burgers}$).
Return these four results as a list of floating-point numbers: [avg_a, avg_b, diff, total_change].
Constraints
- $1 \le \text{total_burgers} \le 10^9$
Examples
Example 1
Input: compare_average_profit(1000000)
Output: [3, 2.6, -0.3999999999999999, -399999.9999999999]
Hints
Hint 1
Determine the unit margin for each burger variant by subtracting unit cost from price.
Hint 2
Convert the Vegan:Regular ratio of 2:3 into unit proportions (Vegan = 2/5, Regular = 3/5).
Hint 3
Multiply the unit profit difference by total_burgers to get total profit impact.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To solve this problem, we evaluate unit-level profitability step-by-step:
-
Calculate Unit Margins:
- $\text{Profit}_{\text{Regular}} = $4 - $1 = $3$
- $\text{Profit}_{\text{Vegan}} = $4 - $2 = $2$
-
Scenario A Average Profit:
- Since only Regular burgers are sold, average profit $\bar{\pi}_A = $3.00$.
-
Scenario B Average Profit:
- The ratio of Vegan to Regular is $2:3$, meaning $2 + 3 = 5$ total parts.
- Vegan proportion = $\frac{2}{5} = 0.4$, Regular proportion = $\frac{3}{5} = 0.6$.
- $\bar{\pi}_B = 0.6 \times $3 + 0.4 \times $2 = $1.80 + $0.80 = $2.60$.
-
Profit Differences:
- Per-unit change = $\bar{\pi}_B - \bar{\pi}_A = $2.60 - $3.00 = -$0.40$.
- Total change = $-$0.40 \times \text{total_burgers}$.
Complexity
- Time Complexity: $O(1)$ since only standard arithmetic calculations are performed.
- Space Complexity: $O(1)$ auxiliary memory required.
from typing import List
def compare_average_profit(total_burgers: int) -> List[float]:
regular_profit = 4 - 1
vegan_profit = 4 - 2
average_a = float(regular_profit)
average_b = (3 * regular_profit + 2 * vegan_profit) / 5.0
difference = average_b - average_a
total_change = difference * total_burgers
return [average_a, average_b, difference, total_change]