Filesystem Entity Total Size
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google flagged this one in September 2026, and the input size is the whole story: up to 200000 entities means you can't rescan the arrays for every directory you visit. The task is to total the file sizes under a queried directory from parallel arrays of IDs, parents, types and sizes. It's a tree build plus one traversal, dressed up as a filesystem. Nothing exotic. If you blank on the setup under the clock, StealthCoder is the safety net running invisibly during the live OA. But the pattern is short enough to own before you sit down.
The problem
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) → long Examples Example 1 ids = ["root","d","a","b"] parentIds = ["","root","d","d"] types = ["DIRECTORY","DIRECTORY","FILE","FILE"] sizes = [0,0,5,6] queryId = "root" return = 11 The 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 = 5 The 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; queryId exists. The entities form one valid rooted tree with exactly one root directory whose parent is the empty string. Every file size is between 0 and 1000000000; directory stored sizes are 0. The requested total fits in a signed 64-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build a children map once: parentId to a list of child indexes, plus an ID to index map. That's O(n). Then traverse from queryId and sum sizes of FILE nodes only. The brute force trap is scanning all n entities to find children at every directory, which goes quadratic and dies at 200000. The second trap is recursion depth. A chain of 200000 nested directories will overflow the stack in most languages, so use an explicit stack or iterative DFS. Third, use a 64-bit accumulator, since 200000 files at 1000000000 each overflows 32-bit. If the query is a file, return its own size. Directory stored sizes are 0, but skip them by type anyway. The follow-ups (postorder subtotals, paged traversal) are discussion only. If the live OA rattles you, StealthCoder can hand you the iterative version fast.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Filesystem Entity Total Size cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Filesystem Entity Total Size FAQ
What's the trick in the Filesystem Entity Total Size problem?+
Build an adjacency map from parent ID to children once, then traverse from queryId and sum FILE sizes. Never search the arrays per directory. That keeps it linear at 200000 entities instead of quadratic.
Will recursion work here?+
Risky. The tree can be a 200000-deep chain, which overflows the call stack in many languages. Use an explicit stack for iterative DFS, or BFS with a queue. Both are linear and safe.
What edge cases should I test?+
Query is a file (return its own size). Query is the root. Query is an empty directory (return 0). Large sizes where the sum exceeds 32-bit, so use a long. Also the root's empty-string parent shouldn't be treated as a real ID.
Do I need to implement the follow-ups?+
No. The statement says they're discussion variants and the judged function gets full arrays and returns one total. Know them for talk: postorder subtotals for all directories, and paged traversal with continuation tokens and checkpoints.
How do I prepare for this in 48 hours?+
Write the children-map plus iterative DFS version from scratch twice. Then practice explaining postorder subtotals out loud. Tree aggregation from parent arrays shows up a lot, and this problem is one clean instance of it.