Parse and Order Structured Logs
Updated
Problem statement
You are given an array of log lines. Keep only the valid lines. A valid line has exactly four fields, separated by the pipe character:
timestamp|level|service|message
timestampis a whole number written with digits only. It is either0or starts with a digit from1through9, so it has no leading zeros. Its value is at most1000000000.levelis exactlyINFO,WARN, orERROR.serviceis not empty and contains only English letters, digits, underscores, or hyphens.messageis not empty. No field contains the pipe character.
Skip every invalid line. For each valid line, return one row [timestamp, level, service, message]. Sort the rows by timestamp as a number, smallest first. When two timestamps are equal, keep the rows in the order they had in the input.
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"]]Sorted by timestamp, the row with 2 comes first. The two rows with timestamp 10 keep their input order, so INFO comes before WARN.
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"]]Three lines are skipped. The first has a timestamp with a leading zero. The third has a level, DEBUG, that is not allowed. The fourth has an empty message. The other two lines are valid, and they are already sorted by timestamp.
Constraints
0 <= logs.length <= 100000.1 <= logs[i].length <= 500.- Every input character is printable ASCII.
More Coinbase interview questions
- Scheduled Payments with Account AliasesONSITE INTERVIEW · Seen Oct 2026
- Dynamic Fixed-K Score LeaderboardONSITE INTERVIEW · Seen Aug 2026
- Minimum Days for Fixed-Order Tasks with CooldownONSITE INTERVIEW · Seen Aug 2026
- Sliding-Window Averages as Reduced FractionsONSITE INTERVIEW · Seen Aug 2026
- Select Transactions with Child-Pays-for-Parent PackagesPHONE SCREEN · Seen Oct 2026
- Task Management SystemOA · Seen Sep 2026
- Cloud Compute Credits SystemOA · Seen Aug 2026
- Banking System with Historical SnapshotsOA · Seen Aug 2026
- Find the Length of the Longest Common PrefixOA · Seen Oct 2024
- Lexicographically Smallest Topological OrderPHONE SCREEN · Seen Apr 2022
Similar questions at other companies
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Response Time Stats by URL PatternGusto · ONSITE INTERVIEWGusto interview questions
- Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Group AnagramsAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
- Keyboard Autocomplete SuggestionsLyft · ONSITE INTERVIEWLyft interview questions
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW