PIXELBANKv9.1.0
Menu

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:

Input:
print(error_rate({200: 90, 500: 5, 503: 5}))
Output:
0.1
Reasoning:
  • Calculate the total number of requests by summing all counts in the histogram: 90+5+5=10090 + 5 + 5 = 100.
  • Identify and sum the counts for status codes in the 5xx range (500–599) to find the total errors: 5+5=105 + 5 = 10.
  • Compute the error rate by dividing the number of errors by the total requests: 10100=0.1\frac{10}{100} = 0.1.
  • Round the result to 4 decimal places, which remains 0.10.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.
πŸ”’

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.

solution.py

Test Results

0/0
Run code to see test results.