Average downtime per stop
Support wants a single number for "how long stops usually take", averaged over every recorded event.
- The average is the total duration divided by the count, truncated down.
- With no events at all the average is treated as zero.
meanDowntime(durations: list<int>) → int
C++ 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.
Where you start
int meanDowntime(std::vector<int> durations) {
}
Worked examples
| Call | Result |
|---|---|
meanDowntime(std::vector<int>{10, 20, 30}) | 20 |
meanDowntime(std::vector<int>{5, 6}) | 5 |
meanDowntime(std::vector<int>{}) | 0 |
meanDowntime(std::vector<int>{100}) | 100 |
Hint
Sum then divide by the length; guard the empty case.
Reference solution in C++
int meanDowntime(std::vector<int> durations) {
if (durations.empty()) return 0;
int sum = 0;
for (int d : durations) sum += d;
return sum / (int) durations.size();
}