L2 · Working engineerAdvanced L2~6 min · 4 tests#87

Process a CSV: Average Salary

Read CSV text with Python's csv.DictReader and compute the average salary, converting types and handling an empty file. Real-world data task, tested.

The problem

csv_text is CSV with a header row name,salary. Return the average salary as a float, or 0.0 if there are no data rows.

Use the csv module rather than splitting on commas yourself.

Examples

  1. Example 1

    Input

    average_salary('name,salary\nJohn,50000\nAlice,70000\nBob,60000')

    Expected output

    60000.0
  2. Example 2

    Input

    average_salary('name,salary\nA,100')

    Expected output

    100.0

+ 2 hidden tests on Submit — header only, quoted comma.

Edge cases to ask about

  • Header only
  • Quoted fields with commas
  • Non-numeric salary

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · average_salary
    ⌘/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 read why.

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

    Pick both to reveal the answer.

    From brute force to optimal

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

    ApproachTimeSpaceIdea
    csv.DictReaderO(n)O(1)Streams rows; handles quoting and embedded commas.
    Walkthrough of the optimal approach (try it yourself first)

    csv.DictReader(io.StringIO(text)) yields one dict per row, keyed by header. Keep a running total and count rather than building a list — this version would work on a 10 GB file.

    line.split(",") breaks on "Smith, J"; the csv module handles quoting, which is what the hidden test checks.

    Complexity: O(n) time, O(1) space. Rows are read one at a time; only a running total and count are kept.

    Reveal the reference solution
    import csv
    import io
    
    def average_salary(csv_text):
        reader = csv.DictReader(io.StringIO(csv_text))
        total = count = 0
        for row in reader:
            total += float(row["salary"])
            count += 1
        return total / count if count else 0.0

    Follow-ups interviewers ask

    • Average per department.
    • Skip rows with an invalid salary and report them.

    Frequently asked interview questions

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

    What is the time complexity of Process a CSV: Average Salary in Python?

    The optimal solution runs in O(n) time and O(1) auxiliary space. Rows are read one at a time; only a running total and count are kept.

    What follow-up questions do interviewers ask about Process a CSV: Average Salary?

    Average per department. Skip rows with an invalid salary and report them.