Practice/Google/File System Size Analysis and Reporting
CodingMust
You are building a disk space analyzer tool that helps users understand storage usage across their file system. Given a hierarchical representation of a file system with folders and files, implement three key analysis functions:
The file system is represented as a nested dictionary/object structure where each node has:
type field that is either "file" or "folder"name field with the entity namesize field (integer representing bytes)children field (list/array of child nodes)calculate_total_size(filesystem, path) that returns the total size of all files under the specified pathfind_largest_files(filesystem, path, k) that returns the k largest files directly in the path (non-recursive)find_largest_subfolders(filesystem, path, k) that returns the k largest immediate child folders with their total sizesExample 1: Calculate Total Size
` Input: filesystem = { "type": "folder", "name": "root", "children": [ {"type": "file", "name": "readme.txt", "size": 150}, { "type": "folder", "name": "src", "children": [ {"type": "file", "name": "main.py", "size": 500} ] } ] } path = "root"
Output: 650 Explanation: Total includes readme.txt (150) + main.py (500) = 650 bytes `
Example 2: Find Largest Files
` Input: filesystem = { "type": "folder", "name": "documents", "children": [ {"type": "file", "name": "report.pdf", "size": 5000}, {"type": "file", "name": "notes.txt", "size": 200}, {"type": "file", "name": "presentation.pptx", "size": 8000}, { "type": "folder", "name": "archive", "children": [ {"type": "file", "name": "old.doc", "size": 10000} ] } ] } path = "documents" k = 2
Output: [("presentation.pptx", 8000), ("report.pdf", 5000)] Explanation: Only files directly in documents are considered. The file in archive is excluded. `
Example 3: Find Largest Subfolders
` Input: filesystem = { "type": "folder", "name": "home", "children": [ { "type": "folder", "name": "music", "children": [ {"type": "file", "name": "song1.mp3", "size": 3000}, {"type": "file", "name": "song2.mp3", "size": 4000} ] }, { "type": "folder", "name": "videos", "children": [ {"type": "file", "name": "movie.mp4", "size": 15000} ] } ] } path = "home" k = 1
Output: [("videos", 15000)] Explanation: videos folder has the largest total size (15000 > 7000 from music) `