KatInterview

Maximize Hourly Output Revenue for a Three-Stage Assembly Line

Dynamic ProgrammingAuto-graded · 24 test cases
HardAuto-gradedDynamic Programmingknapsackpareto-frontierbottleneck

Last updated

Tests budgeted 0/1 selection with a bottleneck objective, requiring Pareto-frontier pruning over a four-dimensional knapsack state space.

Three-Stage Assembly Line: Maximum Hourly Revenue

You are designing a production pipeline that consists of exactly three stage types: Feeders (type 1 or "F"), Processors (type 2 or "P"), and Sealers (type 3 or "S"). A catalog of available units is provided; each unit belongs to one stage type and has an installation cost and a rated capacity (units produced per hour).

How throughput is determined

The pipeline's actual output rate equals the capacity of its weakest stage — the minimum of the total capacity across all installed Feeders, the total capacity across all installed Processors, and the total capacity across all installed Sealers. If no units are installed for a stage type, that stage's total capacity is zero, so the pipeline produces nothing.

Hourly revenue model

Each unit produced sells at sale_price and incurs a variable_cost to produce. Every installed unit also contributes a recurring hourly ownership cost equal to its installation cost multiplied by amortization_rate. The net hourly profit for a given selection is therefore:

net_margin  = sale_price − variable_cost
pipeline_output = min(total_feeder_cap, total_processor_cap, total_sealer_cap)
hourly_profit = net_margin × pipeline_output − amortization_rate × total_installation_cost

Your task

Select a subset of the catalog (each unit used at most once) such that:

  • The total installation cost does not exceed budget.
  • hourly_profit is maximized.

Return the maximum achievable hourly profit as a float. Selecting no units is always allowed, so the answer is never negative.

Input details

  • units — a list of [unit_type, install_cost, capacity] triples.
    • unit_type may be the integer 1, 2, or 3, or the string "F" / "Feeder" / "Feeders" (case-insensitive) for type 1, "P" / "Processor" / "Processors" for type 2, "S" / "Sealer" / "Sealers" for type 3.
    • Units with an unrecognized type, a negative install_cost, or an install_cost exceeding budget are silently ignored.
  • budget — a non-negative integer; maximum total installation cost allowed.
  • sale_price — revenue per unit produced (float).
  • variable_cost — production cost per unit produced (float).
  • amortization_rate — fraction of each unit's installation cost charged per hour (float ≥ 0).

Constraints

  • 0 ≤ budget ≤ 10^6
  • 1 ≤ len(units) ≤ 2 000
  • 0 ≤ install_cost ≤ 10^6 per unit
  • 1 ≤ capacity ≤ 10^4 per unit
  • 0 ≤ variable_cost ≤ sale_price ≤ 10^4
  • 0 ≤ amortization_rate ≤ 1
  • The catalog may contain duplicate units (same type, cost, and capacity); each copy is a distinct physical unit and may be selected independently.
  • Zero-cost units are valid and must be considered.

Examples

Example 1

Input: best_pipeline_profit([[1, 10, 10], [2, 10, 10], [3, 10, 10]], 30, 5, 1, 0.1)

Output: 37

Example 2

Input: best_pipeline_profit([[1, 5, 100], [2, 5, 100], [3, 5, 100], [1, 4, 50], [2, 4, 50], [3, 4, 50]], 15, 3, 1, 0.2)

Output: 197

Example 3

Input: best_pipeline_profit([[1, 3, 10], [2, 3, 10], [3, 3, 10]], 8, 10, 10, 1)

Output: 0

Example 4

Input: best_pipeline_profit([[1, 0, 5], [2, 0, 7], [3, 0, 6]], 0, 2, 0, 0)

Output: 10

Hints

Hint 1

The profit formula only depends on four aggregate numbers per selection: total installation cost and the total capacity of each of the three stage types. Two selections with identical aggregates are interchangeable, so your DP state only needs those four numbers.

Hint 2

After processing each unit, prune any state that is weakly dominated: if state A costs no more than state B and has at least as much capacity in every stage type (and is strictly better in at least one dimension), state B can never lead to a better outcome than A and can be dropped.

Hint 3

If the net margin (sale_price − variable_cost) is ≤ 0, the empty selection wins immediately. Also remember that a missing stage type caps pipeline output at zero, so a selection that installs nothing for any one stage type earns zero throughput revenue.