Log File Analyzer - Problem
You are building a log analysis tool for a web server. Given an array of web server log lines, parse each line to extract the IP address, timestamp, URL path, and HTTP status code.
After parsing all log lines, generate a summary report containing:
- The top 3 IP addresses by request count (most frequent first)
- The overall error rate as a percentage (status codes 400-599 are errors)
Each log line follows this format:
IP - - [timestamp] "GET /path HTTP/1.1" status_code sizeExample: 192.168.1.1 - - [10/Oct/2023:13:55:36] "GET /api/users HTTP/1.1" 200 1234
Return the result as an object with topIPs (array of IP strings) and errorRate (number rounded to 1 decimal place).
Input & Output
Example 1 — Basic Web Server Logs
$
Input:
logs = ["192.168.1.1 - - [10/Oct/2023:13:55:36] \"GET /api/users HTTP/1.1\" 200 1234", "10.0.0.1 - - [10/Oct/2023:13:56:10] \"POST /api/login HTTP/1.1\" 404 567", "192.168.1.1 - - [10/Oct/2023:13:57:22] \"GET /dashboard HTTP/1.1\" 500 890"]
›
Output:
{"topIPs": ["192.168.1.1", "10.0.0.1"], "errorRate": 66.7}
💡 Note:
192.168.1.1 appears 2 times (most frequent), 10.0.0.1 appears 1 time. Out of 3 requests, 2 have error status codes (404, 500), so error rate is 2/3 = 66.7%
Example 2 — All Successful Requests
$
Input:
logs = ["203.0.113.1 - - [10/Oct/2023:14:00:00] \"GET /home HTTP/1.1\" 200 2048", "203.0.113.1 - - [10/Oct/2023:14:01:00] \"GET /about HTTP/1.1\" 301 512"]
›
Output:
{"topIPs": ["203.0.113.1"], "errorRate": 0.0}
💡 Note:
Only one unique IP with 2 requests. Both status codes (200, 301) are successful (not in 400-599 range), so error rate is 0%
Example 3 — Multiple IPs with Ties
$
Input:
logs = ["192.168.1.1 - - [10/Oct/2023:15:00:00] \"GET /api HTTP/1.1\" 200 1024", "10.0.0.1 - - [10/Oct/2023:15:01:00] \"POST /submit HTTP/1.1\" 403 256", "172.16.0.1 - - [10/Oct/2023:15:02:00] \"GET /data HTTP/1.1\" 200 3072"]
›
Output:
{"topIPs": ["192.168.1.1", "10.0.0.1", "172.16.0.1"], "errorRate": 33.3}
💡 Note:
Three IPs each appear once (3-way tie). One error status code (403) out of 3 total requests gives error rate of 1/3 = 33.3%
Constraints
- 0 ≤ logs.length ≤ 104
- Each log line follows the format: IP - - [timestamp] "METHOD /path HTTP/1.1" status_code size
- IP addresses are valid IPv4 format
- Status codes are 3-digit integers (100-599)
- Error status codes are in range 400-599
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code