Problem · Array

Parse and Order Structured Logs

Learn this problem
MediumCoinbase logoCoinbaseFULLTIMEONSITE INTERVIEW

Problem statement

Parse an array of structured log lines. A valid line has exactly four pipe-separated fields:

timestamp|level|service|message

  • timestamp is a canonical nonnegative decimal integer: it is 0 or begins with a digit from 1 through 9, and its value is at most 1000000000.
  • level is exactly INFO, WARN, or ERROR.
  • service is nonempty and contains only English letters, digits, underscores, or hyphens.
  • message is 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.

More Coinbase problems

drafts saved locally
public String[][] parseLogs(String[] logs) {
    // write your code here
}
logs["10|INFO|api|started","2|ERROR|db|down","10|WARN|api|slow"]
expected[["2", "ERROR", "db", "down", "10", "INFO", "api", "started", "10", "WARN", "api", "slow"]]
checking account