Top N Frequent Errors

Quick Overview

Parse log lines in the form [LEVEL] message, keep only the ERROR entries, and return the n most frequent error messages with ties broken alphabetically. It tests string parsing, hash map counting and top-k selection with a sort or a bounded heap.

Top N Frequent Errors

Company: Tesla

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of log entries `logs` and an integer `n`, parse the log entries to identify error messages and return the top `n` most frequent error messages sorted by frequency in descending order. Each log entry follows the format `"[LEVEL] message"` where `LEVEL` can be `INFO`, `WARN`, `ERROR` or `DEBUG`. Only entries with the `ERROR` level should be considered. If two error messages have the same frequency, sort them lexicographically in ascending order. If there are fewer than `n` distinct error messages, return all of them. ### Function Signature ```python def top_n_frequent_errors(logs: list[str], n: int) -> list[str]: ``` ### Rules - An entry counts only when it starts with `[ERROR] `, the level in brackets followed by one space. The message is everything after that prefix. - Messages are compared exactly, so case and spacing matter: `"Disk full"` and `"disk full"` are two different messages. - Ties in frequency are broken by ascending lexicographic order on character codes, which is Python's default string comparison (uppercase letters sort before lowercase ones). - If no entry has the `ERROR` level, return an empty list. ### Constraints - `1 <= len(logs) <= 10^5` - `1 <= n <= 10^4` - Each log entry is a non-empty string in the format `"[LEVEL] message"`. - `LEVEL` is one of `INFO`, `WARN`, `ERROR`, `DEBUG`. - The message part contains only printable ASCII characters. ### Examples ```text Input: logs = ["[ERROR] Null pointer exception","[INFO] Server started","[ERROR] Disk full","[ERROR] Null pointer exception","[WARN] Low memory","[ERROR] Disk full","[ERROR] Disk full","[ERROR] Timeout","[DEBUG] Checkpoint reached"], n = 2 Output: ["Disk full","Null pointer exception"] ``` There are 3 distinct error messages: "Disk full" (3 times), "Null pointer exception" (2 times) and "Timeout" (1 time). The top 2 are "Disk full" and "Null pointer exception". ```text Input: logs = ["[ERROR] Connection refused","[ERROR] Connection refused","[ERROR] Auth failed","[ERROR] Auth failed","[INFO] Request processed","[ERROR] Timeout"], n = 3 Output: ["Auth failed","Connection refused","Timeout"] ``` "Connection refused" and "Auth failed" both appear 2 times and "Timeout" appears once. The first two are tied, so they are sorted lexicographically and "Auth failed" comes before "Connection refused". ```text Input: logs = ["[INFO] Server started","[WARN] Low memory"], n = 1 Output: [] ``` No entry has the `ERROR` level, so there are no error messages to return.

Overview: Parse log lines in the form [LEVEL] message, keep only the ERROR entries, and return the n most frequent error messages with ties broken alphabetically. It tests string parsing, hash map counting and top-k selection with a sort or a bounded heap.

Tesla logo
Tesla
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of log entries logs and an integer n, parse the log entries to identify error messages and return the top n most frequent error messages sorted by frequency in descending order.

Each log entry follows the format "[LEVEL] message" where LEVEL can be INFO, WARN, ERROR or DEBUG. Only entries with the ERROR level should be considered.

If two error messages have the same frequency, sort them lexicographically in ascending order. If there are fewer than n distinct error messages, return all of them.

Function Signature

def top_n_frequent_errors(logs: list[str], n: int) -> list[str]:

Rules

  • An entry counts only when it starts with [ERROR] , the level in brackets followed by one space. The message is everything after that prefix.
  • Messages are compared exactly, so case and spacing matter: "Disk full" and "disk full" are two different messages.
  • Ties in frequency are broken by ascending lexicographic order on character codes, which is Python's default string comparison (uppercase letters sort before lowercase ones).
  • If no entry has the ERROR level, return an empty list.

Constraints

  • 1 <= len(logs) <= 10^5
  • 1 <= n <= 10^4
  • Each log entry is a non-empty string in the format "[LEVEL] message" .
  • LEVEL is one of INFO , WARN , ERROR , DEBUG .
  • The message part contains only printable ASCII characters.

Examples

Input: logs = ["[ERROR] Null pointer exception","[INFO] Server started","[ERROR] Disk full","[ERROR] Null pointer exception","[WARN] Low memory","[ERROR] Disk full","[ERROR] Disk full","[ERROR] Timeout","[DEBUG] Checkpoint reached"], n = 2
Output: ["Disk full","Null pointer exception"]

There are 3 distinct error messages: "Disk full" (3 times), "Null pointer exception" (2 times) and "Timeout" (1 time). The top 2 are "Disk full" and "Null pointer exception".

Input: logs = ["[ERROR] Connection refused","[ERROR] Connection refused","[ERROR] Auth failed","[ERROR] Auth failed","[INFO] Request processed","[ERROR] Timeout"], n = 3
Output: ["Auth failed","Connection refused","Timeout"]

"Connection refused" and "Auth failed" both appear 2 times and "Timeout" appears once. The first two are tied, so they are sorted lexicographically and "Auth failed" comes before "Connection refused".

Input: logs = ["[INFO] Server started","[WARN] Low memory"], n = 1
Output: []

No entry has the ERROR level, so there are no error messages to return.