Filesystem Entity Total Size
Problem statement
A valid filesystem is supplied by parallel arrays. Entity i has unique ID ids[i], parent ID parentIds[i], type FILE or DIRECTORY, and stored size sizes[i]. The root directory has an empty parent ID. Every other entity's parent is a directory.
A file's total size is its own stored size. A directory's total size is the sum of the stored sizes of every descendant file at any depth. Return the total size of queryId.
Interview Follow-Ups
- Disk-backed traversal: If the filesystem metadata is too large to fit in memory, fetch each directory's children page by page and accumulate file sizes as the pages stream in. Persist each open directory's continuation token and partial subtotal so the traversal can resume from a checkpoint after an interruption.
- All directory subtotals: To compute and store every directory's total, process the tree in postorder. Add each file size or completed child-directory subtotal to its parent, then store the completed subtotal for that directory.
These follow-ups are discussion variants. The judged function still receives the complete arrays and returns only the total for queryId.
Function
totalEntitySize(ids: String[], parentIds: String[], types: String[], sizes: long[], queryId: String) → longExamples
Example 1
ids = ["root","d","a","b"]parentIds = ["","root","d","d"]types = ["DIRECTORY","DIRECTORY","FILE","FILE"]sizes = [0,0,5,6]queryId = "root"return = 11The root contains two files beneath its child directory, so both sizes contribute.
Example 2
ids = ["root","d","a","outside"]parentIds = ["","root","d","root"]types = ["DIRECTORY","DIRECTORY","FILE","FILE"]sizes = [0,0,5,20]queryId = "d"return = 5The file outside directory d is not a descendant of the query.
Constraints
1 <= ids.length <= 200000; all four entity arrays have equal length.- IDs are unique nonempty printable ASCII strings of length at most
60;queryIdexists. - The entities form one valid rooted tree with exactly one root directory whose parent is the empty string.
- Every file size is between
0and1000000000; directory stored sizes are0. - The requested total fits in a signed 64-bit integer.