Extract Error Codes from Log Strings

PYTHON coding challenge · Difficulty: medium · Topic: Regular Expressions · +100 XP

Problem

Function Signature

------------------

def extract_error_codes(logs: list[str]) -> dict:

Problem

-------

Given a list of log strings, extract all

error codes and return a count dictionary.

Error code format: E followed by 4 digits

Examples: E1001, E2034, E9999

Rules:

• A single log line can contain multiple

error codes

• Count total occurrences across all lines

• Return {error_code: count} sorted by

count DESC, then code ASC

Example 1

---------

Input:

[

"2024-01-01 ERROR E1001 disk full",

"2024-01-01 ERROR E2034 timeout",

"2024-01-02 ERROR E1001 disk full",

"2024-01-02 WARN E1001 E3001 retry"

]

Output:

{"E1001": 3, "E3001": 1, "E2034": 1}

Example 2

---------

Input:

["INFO: all systems normal",

"DEBUG: connection ok"]

Output: {} (no error codes)

Example 3 (Edge)

----------------

Input: []

Output: {}

Constraints

-----------

• 0 <= len(logs) <= 50,000

• Each log line <= 500 characters

• Use regex: r'E\d{4}'

Example Input

The data below is already defined — do not redefine it.

`python

logs = [

"2024-01-01 INFO: Service started",

"2024-01-01 ERROR-1001: DB timeout",

"2024-01-02 ERROR-2345 and ERROR-6789 found",

"2024-01-03 INFO: All clear",

]

`

Expected Output

`

['ERROR-1001', 'ERROR-2345', 'ERROR-6789']

`

Notes

What this PYTHON challenge teaches you

“Extract Error Codes from Log Strings” is a medium-level PYTHON challenge focused on Regular Expressions. Working through it gives you hands-on practice with regex, log parsing, string manipulation — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.

Concepts covered

How to approach it

If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:

  1. This is a Regular Expressions problem — review the matching python concept.
  2. Compare your output to the Expected Output — the columns, values and row order must match exactly.

Where this comes up

Variations of this problem have been reported in interviews at Netflix, Uber, DataDog. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.

How to practise it on PySpark.in

Open the challenge, write your Python code in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.

Related PYTHON challenges

Frequently asked questions

Do I need to install Spark or a database to solve this?

No. The PYTHON environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.

Is this challenge free?

Yes - the problem, the sample dataset, the hints and unlimited test runs are free.

What level is it?

It is rated medium and covers Regular Expressions.

Solve this challenge free on PySpark.in