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 size

Example: 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
INPUTALGORITHMRESULTWeb Server Log Lines192.168.1.1 ... GET /api ... 20010.0.0.1 ... POST /login ... 404192.168.1.1 ... GET /dash ... 500203.0.113.1 ... GET /home ... 200Raw log entries withIP, timestamp, URL, status1Parse Each Log LineExtract IP and status code2Update Hash MapCount IP frequency O(1) lookup3Track ErrorsIncrement if status 400-5994Sort & CalculateTop 3 IPs + error percentageIP Hash Map:192.168.1.1: 210.0.0.1: 1203.0.113.1: 1Errors: 2/4 = 50%Analytics ReportTop 3 IP Addresses:1. 192.168.1.1 (2 requests)2. 10.0.0.1 (1 request)3. 203.0.113.1 (1 request)Error Rate:50.0%2 errors out of 4 total requestsKey Insight:Single pass through log data with hash map enables O(1) IP counting and simultaneouserror tracking, avoiding the need for multiple scans through the data.TutorialsPoint - Log File Analyzer | Single Pass with Hash Map
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
89.2K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen