Error Rate from a Status-Code Histogram
Problem Statement
Compute the error rate of a service given a histogram of response status codes, where 5xx codes count as errors.
Background
The error rate is the fraction of requests with a 5xx status among all requests. With zero total requests the rate is 0.0.
Your Task
def error_rate(histogram):
- histogram: dict mapping status code (int) -> count (int).
- Return the fraction of 5xx requests, rounded to 4 decimals.
Input Format
- histogram (dict of int -> int).
Output Format
- A float rounded to 4 decimals.
Sample
print(error_rate({200: 90, 500: 5, 503: 5}))
Output:
0.1
Example
print(error_rate({200: 90, 500: 5, 503: 5}))0.1
- Calculate the total number of requests by summing all counts in the histogram: .
- Identify and sum the counts for status codes in the 5xx range (500–599) to find the total errors: .
- Compute the error rate by dividing the number of errors by the total requests: .
- Round the result to 4 decimal places, which remains .
- The final output is 0.1
Constraints
- 5xx means
500 <= code <= 599. - Rate = 5xx count / total; empty or zero total -> 0.0.
- Round to 4 decimals.