Total File Size
Asked at Datadog
Reported by the problem's creator, not verified
A file system is modelled as a tree of entries. Each entry is a dictionary of one of two kinds:
- a file:
{"type": "file", "size": n}, wherenis its size in bytes; - a directory:
{"type": "dir", "children": [...]}, wherechildrenis a list of entries and may be empty.
A directory's children list may contain both files and other directories, nested to any depth. A directory has no size of its own. A file's size is its size field.
Examples
Example 1
Input:
{"entry":{"type":"dir","children":[{"size":100,"type":"file"},{"size":200,"type":"file"}]}}Output:
300Explanation: The directory contains two files of 100 and 200 bytes and nothing else, so the total is 300.
Example 2
Input:
{"entry":{"size":500,"type":"file"}}Output:
500Explanation: A file has no children, so its total is just its own size.
Example 3
Input:
{"entry":{"type":"dir","children":[{"size":10,"type":"file"},{"type":"dir","children":[{"size":40,"type":"file"},{"type":"dir","children":[{"size":5,"type":"file"}]}]}]}}Output:
55Explanation: The top directory holds a file of 10 bytes and a nested directory. That nested directory holds a file of 40 bytes and a deeper directory holding a file of 5 bytes, so the total is 10 + 40 + 5 = 55.