The File Size Profiler
A medium Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.
- Domain
- Python
- Difficulty
- medium
- Seniority
- L3
Problem
Given entries (list of 'filepath size_bytes timestamp' strings), sum size_bytes per file extension (everything after the last '.' in the filename). Files without an extension (no '.' in basename) are grouped under the key 'no_ext'. Extension matching is case-insensitive, so 'report.CSV' counts toward 'csv'. Return a dict mapping each lowercased extension (or 'no_ext') to the total size in bytes.
File types and their disk footprint. One type dominates.
Example
Input:
entries = ["/var/log/app.log 46080 1704067200","/var/log/error.log 1024 1704067201","/data/export.csv 102400 1704067202","/usr/bin/python3 8192 1704067203","/data/report.CSV 51200 1704067204"]
Output:
{"log":47104,"csv":153600,"no_ext":8192}Worked solution and explanation
Why this problem exists in real interviews
This tests string parsing combined with grouping aggregation, a common log analysis pattern. It probes file path parsing, extension extraction, and case-insensitive grouping.
Break down the requirements
Step 1: Parse each metadata string
Split each entry by spaces to extract the filepath and the size. Take the basename (after the last '/'), then extract the file extension from it.
Step 2: Handle files without extensions
If no dot exists in the filename, group under 'no_ext'.
Step 3: Aggregate sizes by extension, case-insensitive
Convert extensions to lowercase before grouping, so report.CSV and export.csv both accumulate under 'csv'.
The solution
def size_by_extension(entries: list[str]) -> dict[str, int]:
totals = {}
for entry in entries:
parts = entry.split()
filepath = parts[0]
size = int(parts[1])
filename = filepath.split('/')[-1]
if '.' in filename:
ext = filename.rsplit('.', 1)[1].lower()
else:
ext = 'no_ext'
if ext in totals:
totals[ext] += size
else:
totals[ext] = size
return totalsCommon follow-up questions
- What if the metadata format varies? (Tests robust parsing with error handling for malformed entries.)
- How would you find the top-3 extensions by total size? (Tests sorting the aggregated dict by value.)
- What if you needed average file size per extension? (Tests tracking both sum and count for each group.)