A Node.js server writes one line per request: method, path, status code and response time in milliseconds. Produce a short summary:
- For each status class that occurs, in the order
2xx,3xx,4xx,5xx, print<class>: <count> requests, avg <avg> ms, where avg is the mean response time rounded down to an integer. - Finally print
slowest: <method> <path> <ms> msfor the slowest request (the earliest line if tied).
Constraints
- 1 ≤ n ≤ 10^5
- Status codes are between 200 and 599; response times are integers between 0 and 10^6.
Input and output
- Input
- Line 1: n Next n lines: METHOD PATH STATUS MS
- Output
- The summary lines described above.
Examples
Input 1
5
GET /api/colleges 200 35
POST /api/login 401 12
GET /api/colleges/7 200 48
GET /old 301 3
GET /api/report 500 950Output
2xx: 2 requests, avg 41 ms
3xx: 1 requests, avg 3 ms
4xx: 1 requests, avg 12 ms
5xx: 1 requests, avg 950 ms
slowest: GET /api/report 950 ms2xx: (35 + 48) / 2 = 41.5, rounded down to 41.
Input 2
2
GET /a 404 10
GET /b 404 10Output
4xx: 2 requests, avg 10 ms
slowest: GET /a 10 msA tie for slowest goes to the earlier line.
Show hints
- The class is the first digit of the status code.
- Keep a running count and sum per class instead of storing every time.
Show the explanation
One pass: for each line, take the first digit of the status as the class and add to that class's count and sum; track the slowest request, replacing it only when a time is strictly greater (that keeps the earliest on ties).
Then print the classes in fixed order, skipping empty ones, with Math.floor(sum / count).
In production you would read the log as a stream (readline over fs.createReadStream) so a multi-gigabyte file never sits in memory, and you would usually report percentiles (p95, p99) rather than averages, because a few very slow requests are hidden by a mean.
Original UniCircle question. Last reviewed 22 Oct 2026.
