What share of requests failed
A status page shows the error rate as a whole percentage, because nobody reads four decimal places at a glance.
- Rounded to the nearest whole percent, halves going up.
- No requests at all means no rate: return 0.
- More errors than requests is impossible data — treat it as 100.
errorRate(errors: int, total: 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.
Where you start
func errorRate(errors int, total int) int {
}
Worked examples
| Call | Result |
|---|---|
errorRate(5, 100) | 5 |
errorRate(1, 3) | 33 |
errorRate(2, 3) | 67 |
errorRate(0, 100) | 0 |
Hint
Add half the divisor before dividing, then cap.
Reference solution in Go
func errorRate(errors int, total int) int {
if total <= 0 || errors <= 0 {
return 0
}
pct := (errors*100 + total/2) / total
if pct > 100 {
return 100
}
return pct
}