AlgoMaster Logo
AlgoMasterCalculate System Availabilityeasy

Calculate System Availability

easy

The availability of a system depends on how its components are connected. In a series topology, every component must be available for the system to work. In a parallel topology, the components are redundant, so the system works while at least one component remains available.

Design an AvailabilityCalculator class:

  • AvailabilityCalculator() creates a stateless calculator.
  • double calculate(double[] uptimes, String mode) returns the combined availability for all components, rounded to 5 decimal places.

Each value in uptimes is an independent component's availability as a fraction from 0 to 1:

  • When mode is "series", return the product of all component uptimes.
  • When mode is "parallel", multiply the component failure probabilities, (1 - uptime), and return 1 minus that product.

Do not round intermediate products. Round only the final combined availability.

Example 1:

Input:

Output:

Explanation: All three components must be available. Their combined availability is 0.99 × 0.99 × 0.99 = 0.970299, which rounds to 0.9703.

Example 2:

Input:

Output:

Explanation: Each component fails with probability 0.01. Both fail together with probability 0.01 × 0.01 = 0.0001, so the redundant pair is available with probability 1 - 0.0001 = 0.9999.

Constraints

  • 1 <= uptimes.length <= 100
  • 0 <= uptimes[i] <= 1
  • mode is either "series" or "parallel".
  • Component availability events are independent.
  • At most 100 calls are made to calculate.
  • Answers are accepted within 10^-5 of the expected result.
Hints

Loading...
CallReturns
new AvailabilityCalculator()null
calculate([0.99,0.99,0.99], "series")0.9703

All three components must be up, so the availability is 0.99 × 0.99 × 0.99 = 0.970299, which rounds to 0.9703.

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