L2 · Working engineerAdvanced L2~5 min · 5 tests#86

Parse Logs and Count Log Levels

Parse log lines and count each level (INFO, ERROR, WARN) with split and a Counter in O(n), skipping blank or malformed lines. Practical Python test.

The problem

Each log line starts with its level followed by a space and a message, e.g. "ERROR Database failed". Return a dict of {level: count}.

Ignore blank lines.

Examples

  1. Example 1

    Input

    count_levels(['INFO User logged in', 'ERROR Database failed', 'INFO Request completed', 'ERROR Timeout'])

    Expected output

    {'INFO': 2, 'ERROR': 2}
  2. Example 2

    Input

    count_levels(['WARN disk 90%'])

    Expected output

    {'WARN': 1}

+ 3 hidden tests on Submit — blank lines ignored, level with no message.

Edge cases to ask about

  • Blank lines
  • Level with no message

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · count_levels
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    Measure it

    Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Split once, countO(n)O(k)maxsplit=1 avoids splitting the whole message.
    Walkthrough of the optimal approach (try it yourself first)

    Strip each line, skip blanks, take the first token with split(maxsplit=1)[0], and count. Accepting any iterable (not just a list) means the same function works on an open file, line by line, without loading it — see questions 90 and 91.

    For structured logs, a regex like ^(?P<level>[A-Z]+)\s makes malformed lines explicit.

    Complexity: O(n) time, O(k) space. One pass over all characters; k distinct levels are stored.

    Reveal the reference solution
    def count_levels(lines):
        counts = {}
        for line in lines:
            line = line.strip()
            if not line:
                continue
            level = line.split(maxsplit=1)[0]
            counts[level] = counts.get(level, 0) + 1
        return counts

    Follow-ups interviewers ask

    • Also return the most common error message.
    • Lines also have a timestamp prefix.

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Parse Logs and Count Log Levels in Python?

    The optimal solution runs in O(n) time and O(k) auxiliary space. One pass over all characters; k distinct levels are stored.

    What follow-up questions do interviewers ask about Parse Logs and Count Log Levels?

    Also return the most common error message. Lines also have a timestamp prefix.