Maximize pipeline throughput by upgrading components within a budget

Quick Overview

Find the largest throughput a pipeline can reach when every component below the target capacity must be upgraded at a per-unit cost, without exceeding a fixed budget. Tests binary search on the answer, monotonic cost functions and choosing safe search bounds.

Maximize pipeline throughput by upgrading components within a budget

Company: Two Sigma

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a system with multiple components arranged in a pipeline. Each component `i` has an initial capacity `capacities[i]` and an upgrade cost `costs[i]` per unit of capacity added. The total throughput of the pipeline is determined by the smallest component capacity (the bottleneck). Given an integer `budget`, return the maximum throughput `T` such that the total cost to upgrade every component to a capacity of at least `T` does not exceed `budget`. Upgrading component `i` from its current capacity `c` to a target `T` costs `max(0, T - c) * costs[i]`. Components with capacity already greater than or equal to `T` require no upgrade. Reaching `T = min(capacities)` needs no upgrade at all, so when the budget cannot pay for anything more, the answer is the minimum initial capacity. ### Function Signature ```python def max_throughput(capacities: list[int], costs: list[int], budget: int) -> int: ``` ### Rules - `capacities[i]` and `costs[i]` describe the same component. - `T` is an integer, and capacity is added in whole units. Capacities can only be raised. ### Constraints - `1 <= len(capacities) == len(costs) <= 10^5` - `1 <= capacities[i] <= 10^9` - `1 <= costs[i] <= 10^4` - `0 <= budget <= 10^13` ### Examples ```text Input: capacities = [3, 5, 4], costs = [1, 2, 1], budget = 6 Output: 5 ``` With `T = 5` the cost is (5 - 3) * 1 + 0 + (5 - 4) * 1 = 3, which is within budget. With `T = 6` the cost is (6 - 3) * 1 + (6 - 5) * 2 + (6 - 4) * 1 = 7, which is over budget. ```text Input: capacities = [10, 10, 10], costs = [1, 1, 1], budget = 0 Output: 10 ``` With a budget of 0 no upgrade is possible, so the throughput equals the minimum capacity, 10. ```text Input: capacities = [1, 8], costs = [3, 1], budget = 20 Output: 7 ``` While `T` is at most 8, only the first component needs upgrading. `T = 7` costs 6 * 3 = 18, and `T = 8` costs 7 * 3 = 21, which is over budget.

Overview: Find the largest throughput a pipeline can reach when every component below the target capacity must be upgraded at a per-unit cost, without exceeding a fixed budget. Tests binary search on the answer, monotonic cost functions and choosing safe search bounds.

Two Sigma logo
Two Sigma
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a system with multiple components arranged in a pipeline. Each component i has an initial capacity capacities[i] and an upgrade cost costs[i] per unit of capacity added. The total throughput of the pipeline is determined by the smallest component capacity (the bottleneck).

Given an integer budget, return the maximum throughput T such that the total cost to upgrade every component to a capacity of at least T does not exceed budget. Upgrading component i from its current capacity c to a target T costs max(0, T - c) * costs[i]. Components with capacity already greater than or equal to T require no upgrade.

Reaching T = min(capacities) needs no upgrade at all, so when the budget cannot pay for anything more, the answer is the minimum initial capacity.

Function Signature

def max_throughput(capacities: list[int], costs: list[int], budget: int) -> int:

Rules

  • capacities[i] and costs[i] describe the same component.
  • T is an integer, and capacity is added in whole units. Capacities can only be raised.

Constraints

  • 1 <= len(capacities) == len(costs) <= 10^5
  • 1 <= capacities[i] <= 10^9
  • 1 <= costs[i] <= 10^4
  • 0 <= budget <= 10^13

Examples

Input: capacities = [3, 5, 4], costs = [1, 2, 1], budget = 6
Output: 5

With T = 5 the cost is (5 - 3) * 1 + 0 + (5 - 4) * 1 = 3, which is within budget. With T = 6 the cost is (6 - 3) * 1 + (6 - 5) * 2 + (6 - 4) * 1 = 7, which is over budget.

Input: capacities = [10, 10, 10], costs = [1, 1, 1], budget = 0
Output: 10

With a budget of 0 no upgrade is possible, so the throughput equals the minimum capacity, 10.

Input: capacities = [1, 8], costs = [3, 1], budget = 20
Output: 7

While T is at most 8, only the first component needs upgrading. T = 7 costs 6 * 3 = 18, and T = 8 costs 7 * 3 = 21, which is over budget.