AlgoMaster Logo
AlgoMasterAnalyze Capacity Bottlenecksmedium

Analyze Capacity Bottlenecks

medium

Every request in a system consumes work from several components. A component may have a high raw capacity but still become the bottleneck when each request asks it to perform several units of work.

Design a CapacityBottleneckAnalyzer class:

  • CapacityBottleneckAnalyzer() creates a stateless analyzer.
  • double maxThroughput(double[] capacities, double[] workPerRequest) returns the maximum end-to-end request throughput, rounded to 5 decimal places.
  • String bottleneck(String[] components, double[] capacities, double[] workPerRequest) returns the name of the component that limits throughput.

The three arrays use aligned indices. For component i:

Every request uses every listed component, so the system throughput is the smallest component throughput. If multiple components have exactly the same smallest ratio, bottleneck returns the alphabetically smallest component name.

Use the raw ratios to select the bottleneck. Round only the final value returned by maxThroughput. Each method call is independent.

Example 1:

Input:

Output:

Explanation: API supports 12000 / 1 = 12000 requests per second, database supports 5000 / 2 = 2500, and cache supports 20000 / 0.5 = 40000. Database is the bottleneck.

Example 2:

Input:

Output:

Explanation: Gateway and search each support 6000 requests per second. Ranking supports 3000, so it limits the system.

Constraints

  • 1 <= capacities.length == workPerRequest.length <= 200
  • For bottleneck, components.length == capacities.length.
  • Component names are unique, non-empty lowercase strings.
  • 0 < capacities[i], workPerRequest[i] <= 10^9
  • A tie means the unrounded mathematical throughput ratios are equal.
  • Answers are accepted within 10^-5 of the expected result.
  • At most 100 total method calls are made.
Hints

Loading...
CallReturns
new CapacityBottleneckAnalyzer()null
maxThroughput([12000,5000,20000], [1,2,0.5])2500
bottleneck(["api","database","cache"], [12000,5000,20000], [1,2,0.5])"database"

The components support 12000, 2500, and 40000 requests per second. The database has the smallest throughput and limits the whole request path to 2500 requests per second.

Run checks these cases. Submit also runs a larger hidden set.