Skip to content
RED SAGSkills
easy15 ptsstringsparsing

Parse an Access Log Line

An HTTP server writes one line per request in the form <timestamp> <METHOD> <path> <status> <duration>ms, for example 2024-03-01T10:00:00Z GET /api/users 200 15ms. Fields are separated by one or more spaces, and the line may have leading or trailing spaces.

Return an object { timestamp, method, path, status, durationMs } where status and durationMs are numbers. Return null if the line is malformed. A line is valid only when it has exactly 5 fields, the method is one of GET, POST, PUT, PATCH, DELETE, the path starts with /, the status is a 3-digit number from 100 to 599, and the duration is digits followed by ms.

Example 1

Input: line = "2024-03-01T10:00:00Z GET /api/users 200 15ms"
Output: {"timestamp":"2024-03-01T10:00:00Z","method":"GET","path":"/api/users","status":200,"durationMs":15}

Example 2

Input: line = "2024-03-01T10:00:00Z FETCH /x 200 1ms"
Output: null
Explanation: FETCH is not an allowed method.

Constraints

  • 0 <= line.length <= 500
Hint 1

Trim the line and split on a whitespace regex such as /\s+/.

Hint 2

Validate each field separately and bail out with null at the first failure.

Good practice for: Backend Developer, DevOps Engineer, Site Reliability Engineer, Cloud Engineer, Data Engineer, Automation Test Engineer

Run checks your code on 3 sample tests. Submit runs all 10 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.