Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Traverse and Process Files

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Traverse and Process Files. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Capgemini Invent delivery pipelines may need to inspect a nested collection of files before processing analytics inputs. Implement a function that traverses an in-memory directory tree, finds files with a requested extension, and returns their paths and sizes.

Use a nested dictionary to represent the directory structure. Each dictionary key is a file or directory name. A dictionary value represents a subdirectory, while an integer value represents a file size in bytes. The input dictionary represents the root directory and has no name of its own.

Return a dictionary with files and total_size keys. files must contain matching file records with path and size fields, ordered lexicographically by full path. Match the extension exactly and case-sensitively. A file matches when its name ends with the requested extension.

Formal Specification

  • Input: tree, a nested dictionary whose values are either dictionaries or non-negative integers, and extension, a non-empty string such as .csv.
  • Output: A dictionary containing files, a list of {"path": string, "size": integer} records, and total_size, the sum of matching file sizes.
  • Directory and file names contain no / characters.

Constraints

  • 1 <= total number of files and directories <= 10^5
  • 0 <= file size <= 10^9
  • Directory nesting depth is at most 1,000
  • Directory and file names contain no / characters
  • The extension is a non-empty, case-sensitive string

Function Signature

def process_files(tree, extension):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output