← 返回博客列表
Bloomberg

Bloomberg Interview Question: Sum CSV Column in Folder

2025-12-18

Problem

There are multiple .csv files scattered across a directory and its subdirectories.

Implement process_path(path):

Example:

.
|-- a
|   `-- b
|       `-- ex.csv
|-- example.csv
`-- ignoreme.log

ex.csv:
a,5,-1,-1,0
a,10,-1,-1,0

example.csv:
b,0,-1,-1,0
b,-3,-1,-1,0

Result: 12

Approach

Use os.walk to traverse the directory and filter by .csv extension.

Python

import os
import csv


def process_csv(filepath: str) -> int:
    total = 0
    with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
        reader = csv.reader(f)
        for row in reader:
            if len(row) >= 2:
                total += int(row[1])
    return total


def process_path(path: str) -> int:
    total = 0
    for root, _, files in os.walk(path):
        for name in files:
            if name.endswith('.csv'):
                total += process_csv(os.path.join(root, name))
    return total

Complexity


Need interview help? Contact OA VO Service

Need real interview questions? Contact WeChat: Coding0201 to get the questions.