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: 90+5+5=100.
- Identify and sum the counts for status codes in the 5xx range (500β599) to find the total errors: 5+5=10.
- Compute the error rate by dividing the number of errors by the total requests: 10010β=0.1.
- Round the result to 4 decimal places, which remains 0.1.
- 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.
1. Background Knowledge
In software observability, an SLO (Service Level Objective) defines the expected reliability of a service. A common metric is the error rate, which quantifies the fraction of requests that fail. In HTTP-based systems, status codes in the 5xx range (500β599) indicate server-side errors, while 2xx and 3xx codes represent successful responses.
A histogram in this context is a dictionary mapping discrete status codes to their occurrence counts. Rather than storing individual request logs, histograms aggregate data, making them memory-efficient for large-scale monitoring. The total number of requests is the sum of all counts in the histogram, and the number of errors is the sum of counts for keys in the 5xx range.
The error rate is defined as:
error_rate=βkβcount(k)βkβ5xxβcount(k)βIf the denominator is zero (no requests recorded), the rate is defined as 0.0 to avoid division by zero.
2. Algorithm Approach
This is a straightforward aggregation problem. The approach involves:
- Iterating over the histogram dictionary.
- Accumulating two sums: total request count and error request count.
- Computing the ratio and handling the zero-division edge case.
- Rounding the result to 4 decimal places.
No sorting, searching, or complex data structures are neededβjust a single pass through the dictionary.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.