Drill

ProblemsTypeScript › machines

Average downtime per stop

mediummachinesTypeScript

Support wants a single number for "how long stops usually take", averaged over every recorded event.

meanDowntime(durations: list<int>) → int

Solve it in the editor →

Where you start

function meanDowntime(durations: number[]): number {
  
}

Worked examples

CallResult
meanDowntime([10,20,30])20
meanDowntime([5,6])5
meanDowntime([])0
meanDowntime([100])100

Hint

Sum then divide by the length; guard the empty case.

Reference solution in TypeScript
function meanDowntime(durations: number[]): number {
  if (!durations.length) return 0;
  let sum = 0;
  for (const d of durations) sum += d;
  return Math.floor(sum / durations.length);
}

The same problem in another language

More machines problems in TypeScript