Parse logs and count error codes
Company: Nuro
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
You are given application log lines. Each line is expected to follow this format:
```
[LEVEL] timestamp, eventName, errorCode, text
```
- `LEVEL` is a single character such as `I`, `W`, `E` inside square brackets (e.g., `[I]`).
- Fields after the level are separated by commas.
- `text` may contain arbitrary characters (assume it does **not** contain a newline).
### Task
Write a Python function that parses a list of log lines and returns the count of each `errorCode`.
### Input
- A list of strings `lines`, where each string is one log line.
### Output
- A dictionary/map from `errorCode` (string) to its occurrence count (integer).
### Requirements / Edge cases
- Trim extra whitespace around comma-separated fields.
- Ignore lines that do not match the expected format (do not crash).
- Treat `errorCode` as a string (e.g., `"404"`, `"E123"`).
### Example
Input:
```
[I] 2026-01-01T00:00:00Z, Login, E123, user not found
[W] 2026-01-01T00:00:01Z, Login, E123, retry
[E] 2026-01-01T00:00:02Z, Payment, P007, declined
```
Output:
```
{ "E123": 2, "P007": 1 }
```
Quick Answer: This question evaluates parsing and sanitizing structured text, extracting specific fields, and performing frequency aggregation of identifiers such as error codes, demonstrating string-processing and robustness skills.
Parse log lines in '[LEVEL] timestamp, eventName, errorCode, text' format and count each errorCode, ignoring malformed lines.
Constraints
- text does not contain newlines
- Malformed lines are ignored
Examples
Input: (['[I] 2026-01-01T00:00:00Z, Login, E123, user not found', '[W] 2026-01-01T00:00:01Z, Login, E123, retry', '[E] 2026-01-01T00:00:02Z, Payment, P007, declined'],)
Expected Output: {'E123': 2, 'P007': 1}
Explanation: Counts repeated codes.
Input: (['bad line', '[EE] t, e, X, text', '[I] t, e, 404, text, with comma'],)
Expected Output: {'404': 1}
Explanation: Ignores malformed lines and keeps text commas.
Input: ([],)
Expected Output: {}
Explanation: No logs.
Input: (['[I] t , event , E123 , text'],)
Expected Output: {'E123': 1}
Explanation: Whitespace is trimmed.
Hints
- Split only the first three commas after the level so text may contain commas.