Problem · Array
Parse and Order Structured Logs
Learn this problemProblem statement
Parse an array of structured log lines. A valid line has exactly four pipe-separated fields:
timestamp|level|service|message
timestampis a canonical nonnegative decimal integer: it is0or begins with a digit from1through9, and its value is at most1000000000.levelis exactlyINFO,WARN, orERROR.serviceis nonempty and contains only English letters, digits, underscores, or hyphens.messageis nonempty. No field contains the pipe character.
Ignore every invalid line. For each valid line, return one row [timestamp, level, service, message]. Sort the returned rows by numeric timestamp in ascending order. When timestamps are equal, preserve the original input order.
Function
parseLogs(logs: String[]) → String[][]Examples
Example 1
logs = ["10|INFO|api|started","2|ERROR|db|down","10|WARN|api|slow"]return = [["2","ERROR","db","down"],["10","INFO","api","started"],["10","WARN","api","slow"]]The timestamp 2 comes first. The two timestamp-10 rows retain their input order.
Example 2
logs = ["01|INFO|api|leading zero","0|INFO|boot_service|ready now","7|DEBUG|api|trace","8|WARN|api|","9|ERROR|db|disk full"]return = [["0","INFO","boot_service","ready now"],["9","ERROR","db","disk full"]]The leading-zero timestamp, unsupported level, and empty message are invalid and ignored.
Constraints
0 <= logs.length <= 100000.1 <= logs[i].length <= 500.- Every input character is printable ASCII.