Problem · String

HTTP Status Log Summary

Learn this problem
MediumApple logoAppleFULLTIMEPHONE SCREEN

Problem statement

Analyze the strings in records. A valid record has exactly four whitespace-separated fields in this order: IPv4 address, non-negative decimal timestamp, API path, and HTTP status code.

A record is valid only when:

  • the IPv4 address has four decimal octets from 0 through 255;
  • the timestamp fits in a signed 64-bit integer and is non-negative;
  • the API path begins with /; and
  • the status is an integer from 100 through 599.

Among valid records, count client errors in the 400 range and server errors in the 500 range. The failure percentage is 100 * (client errors + server errors) / valid records, rounded to exactly two decimal places using ordinary half-up rounding. When there are no valid records, it is 0.00.

Return exactly five strings: valid=N, invalid=N, client_errors=N, server_errors=N, and failure_percentage=P.

Function

summarizeStatusLogs(records: String[]) → String[]

Examples

Example 1

records = ["10.0.0.1 100 /vote 200","10.0.0.2 101 /vote 404","bad 102 /vote 503","10.0.0.3 103 /vote 503"]return = ["valid=3","invalid=1","client_errors=1","server_errors=1","failure_percentage=66.67"]

Three records are valid. Two of those three have a 4xx or 5xx status, so the failure percentage is 66.67.

Example 2

records = ["256.1.1.1 1 /x 200","1.1.1.1 -1 /x 500"]return = ["valid=0","invalid=2","client_errors=0","server_errors=0","failure_percentage=0.00"]

The first IP octet is out of range and the second timestamp is negative, so neither record contributes to status metrics.

Constraints

  • 1 <= records.length <= 2 * 10^5.
  • 1 <= records[i].length <= 500.
  • Records contain printable ASCII characters and whitespace.

More Apple problems

drafts saved locally
public String[] summarizeStatusLogs(String[] records) {
    // Write your code here.
}
records["10.0.0.1 100 /vote 200","10.0.0.2 101 /vote 404","bad 102 /vote 503","10.0.0.3 103 /vote 503"]
expected["valid=3", "invalid=1", "client_errors=1", "server_errors=1", "failure_percentage=66.67"]
checking account