Drill

ProblemsGo › machines

Average downtime per stop

mediummachinesArraysMathGo

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

meanDowntime(durations: list<int>) → int

Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

func meanDowntime(durations []int) int {
	
}

Worked examples

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

Hint

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

Reference solution in Go
func meanDowntime(durations []int) int {
	if len(durations) == 0 {
		return 0
	}
	s := 0
	for _, d := range durations {
		s += d
	}
	return s / len(durations)
}

The same problem in another language

More machines problems in Go