Parse Log Entries and Group Messages by Level in Timestamp Order

Quick Overview

Parse log strings of the form timestamp:level:message and return the messages grouped into INFO, WARNING and ERROR lists, each sorted by timestamp. It tests string splitting, grouping with a hash map, and sorting by a numeric key while keeping empty groups in place.

Parse Log Entries and Group Messages by Level in Timestamp Order

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an array of log entries `logs` where each entry is a string in the format `"timestamp:level:message"`. The `timestamp` is an integer, the `level` is one of `"INFO"`, `"WARNING"`, or `"ERROR"`, and the `message` is a non-empty string. Parse the log entries and return a list of three lists grouped by log level in the order `["INFO", "WARNING", "ERROR"]`. Each group should contain the messages belonging to that level, sorted by their timestamp in ascending order. If a level has no entries, its corresponding list should be empty. ### Function Signature ```python def parse_logs(logs: list[str]) -> list[list[str]]: ``` ### Rules - The timestamp is the text before the first colon, the level is the text between the first and second colon, and the message is everything after the second colon, kept exactly as written (spaces included). - The result always holds exactly three lists, in the order INFO, WARNING, ERROR, even when some of them are empty. - Inside each list, messages appear in ascending numeric order of their timestamps, so a timestamp of `9` comes before `10`. All timestamps are unique, so the order is always unique. ### Constraints - `1 <= logs.length <= 10^4` - Each log entry is in the format `"timestamp:level:message"` - `timestamp` is a non-negative integer - `level` is one of `"INFO"`, `"WARNING"`, `"ERROR"` - `message` is a non-empty string consisting of lowercase and uppercase English letters and spaces - All timestamps are unique ### Examples ```text Input: logs = ["100:INFO:User logged in","50:ERROR:Disk full","75:WARNING:Low memory","200:INFO:User logged out","25:ERROR:Connection lost"] Output: [["User logged in","User logged out"],["Low memory"],["Connection lost","Disk full"]] ``` The INFO messages sorted by timestamp are "User logged in" (100) and "User logged out" (200). WARNING has only "Low memory" (75), and ERROR sorted by timestamp is "Connection lost" (25) then "Disk full" (50). ```text Input: logs = ["10:INFO:Start","20:INFO:Process","30:INFO:End"] Output: [["Start","Process","End"],[],[]] ``` Every entry is INFO, so that list holds all three messages in timestamp order, and the WARNING and ERROR lists are empty. ```text Input: logs = ["10:ERROR:Give up","9:ERROR:Retry"] Output: [[],[],["Retry","Give up"]] ``` Timestamp 9 is smaller than 10, so "Retry" comes first. Comparing the timestamps as strings would put "10" before "9" and return the wrong order.

Overview: Parse log strings of the form timestamp:level:message and return the messages grouped into INFO, WARNING and ERROR lists, each sorted by timestamp. It tests string splitting, grouping with a hash map, and sorting by a numeric key while keeping empty groups in place.

PracHub logo
General
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given an array of log entries logs where each entry is a string in the format "timestamp:level:message". The timestamp is an integer, the level is one of "INFO", "WARNING", or "ERROR", and the message is a non-empty string.

Parse the log entries and return a list of three lists grouped by log level in the order ["INFO", "WARNING", "ERROR"]. Each group should contain the messages belonging to that level, sorted by their timestamp in ascending order. If a level has no entries, its corresponding list should be empty.

Function Signature

def parse_logs(logs: list[str]) -> list[list[str]]:

Rules

  • The timestamp is the text before the first colon, the level is the text between the first and second colon, and the message is everything after the second colon, kept exactly as written (spaces included).
  • The result always holds exactly three lists, in the order INFO, WARNING, ERROR, even when some of them are empty.
  • Inside each list, messages appear in ascending numeric order of their timestamps, so a timestamp of 9 comes before 10 . All timestamps are unique, so the order is always unique.

Constraints

  • 1 <= logs.length <= 10^4
  • Each log entry is in the format "timestamp:level:message"
  • timestamp is a non-negative integer
  • level is one of "INFO" , "WARNING" , "ERROR"
  • message is a non-empty string consisting of lowercase and uppercase English letters and spaces
  • All timestamps are unique

Examples

Input: logs = ["100:INFO:User logged in","50:ERROR:Disk full","75:WARNING:Low memory","200:INFO:User logged out","25:ERROR:Connection lost"]
Output: [["User logged in","User logged out"],["Low memory"],["Connection lost","Disk full"]]

The INFO messages sorted by timestamp are "User logged in" (100) and "User logged out" (200). WARNING has only "Low memory" (75), and ERROR sorted by timestamp is "Connection lost" (25) then "Disk full" (50).

Input: logs = ["10:INFO:Start","20:INFO:Process","30:INFO:End"]
Output: [["Start","Process","End"],[],[]]

Every entry is INFO, so that list holds all three messages in timestamp order, and the WARNING and ERROR lists are empty.

Input: logs = ["10:ERROR:Give up","9:ERROR:Retry"]
Output: [[],[],["Retry","Give up"]]

Timestamp 9 is smaller than 10, so "Retry" comes first. Comparing the timestamps as strings would put "10" before "9" and return the wrong order.