FastPrepHTTP Status Log Summary

HTTP Status Log Summary

Apple logoApple● MediumFULLTIMEPHONE SCREEN

Updated

Problem statement

You are given records, a list of log lines. A valid record has exactly four fields separated by whitespace, in this order: an IPv4 address, a non-negative decimal timestamp, an API path, and an HTTP status code.

A record is valid only when all of these hold:

  • The IPv4 address has four decimal octets, each from 0 through 255.
  • The timestamp is not negative and fits in a signed 64-bit integer.
  • The API path starts with /.
  • The status is an integer from 100 through 599.

Among the valid records, count the client errors, which have a status in the 400 range. Also count the server errors, which have a status in the 500 range.

The failure percentage is 100 * (client errors + server errors) / valid records. Round it to exactly two decimal places, with halves rounded up. If 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. Each N is a count, and P is the failure percentage.

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"]

In the first record, the first octet of the IP address is out of range. In the second record, the timestamp is negative. Both records are invalid, so there are no valid records, no client or server errors, and the failure percentage is zero.

Constraints

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

More Apple Phone Screen questions

Similar questions at other companies

Apple interview experiences

All Apple interview questionsSee Apple hiring insights
Saved
class Solution {
    public String[] summarizeStatusLogs(String[] records) {
        // Write your code here.
    }
}
Checking account…