Total File Size

Asked at Datadog

Reported by the problem's creator, not verified

Import another question

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}, where n is its size in bytes;
  • a directory: {"type": "dir", "children": [...]}, where children is 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

  1. Example 1

    Input: {"entry":{"type":"dir","children":[{"size":100,"type":"file"},{"size":200,"type":"file"}]}}

    Output: 300

    Explanation: The directory contains two files of 100 and 200 bytes and nothing else, so the total is 300.

  2. Example 2

    Input: {"entry":{"size":500,"type":"file"}}

    Output: 500

    Explanation: A file has no children, so its total is just its own size.

  3. 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: 55

    Explanation: 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.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.