FastPrepFile Sizes Under Directory Paths
Problems/File Sizes Under Directory Paths

File Sizes Under Directory Paths

Datadog logoDatadog● MediumFULLTIMEINTERNNEW GRADPHONE SCREENONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The reports ask for a directory's total file size and a follow-up query for a selected path. The array representation, missing-path result, examples, and bounds are specified for practice. The judged core task matches the reports at about 85%.

A filesystem tree is represented by parallel arrays paths and sizes. Entry i is a directory when sizes[i] == -1; otherwise it is a file with that nonnegative size.

For every path in queries, return the total size of all files at or below that path. A file query returns its own size. A directory includes all its descendant files. An empty directory has total size 0. Return -1 for a path that is absent from the tree.

Return results in query order. Path matching is case-sensitive and follows directory boundaries: /ab is not inside /a. The input entries may be in any order, and queries may repeat.

Function

fileSizesByPath(paths: String[], sizes: int[], queries: String[]) → long[]

Examples

Example 1

paths = ["/","/home","/home/me","/home/me/foo.txt","/home/me/abs.txt","/home/haha.css"]sizes = [-1,-1,-1,231,443,52]queries = ["/home","/home/me","/home/me/foo.txt","/missing"]return = [726,674,231,-1]

/home/me contains files of sizes 231 and 443, totaling 674. Adding haha.css gives /home a total of 726. A file query returns its own size, and the missing path returns -1.

Example 2

paths = ["/","/empty","/zero.txt"]sizes = [-1,-1,0]queries = ["/empty","/","/zero.txt"]return = [0,0,0]

The empty directory has total size 0. The only file also has size 0, so the root total is 0.

Example 3

paths = ["/","/a","/ab","/a/f","/ab/g"]sizes = [-1,-1,-1,5,7]queries = ["/a","/ab","/"]return = [5,7,12]

/a/f belongs to /a, while /ab/g belongs to /ab. A shared text prefix does not make /ab a child of /a. The root contains both files and totals 12.

Constraints

  • 1 <= paths.length <= 3000
  • paths.length == sizes.length
  • 0 <= queries.length <= 3000
  • 1 <= paths[i].length <= 200
  • 1 <= queries[i].length <= 200
  • -1 <= sizes[i] <= 1000000000
  • All paths are absolute and canonical. The root is /. Other paths have nonempty slash-separated segments containing only ASCII letters, digits, periods, underscores, and hyphens; a segment cannot be . or .., and there is no trailing slash.
  • The entries have unique paths, include the root as a directory, and include each non-root entry's parent as a directory. Files have no children. A query may name an absent path.

More Datadog problems

  • Query Log MatchPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Delete a Filesystem TreePHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
  • Grouped-Query Attention Forward PassPHONE SCREEN · Seen Jun 2026
All Datadog interview questionsSee Datadog hiring insights
Saved
public long[] fileSizesByPath(String[] paths, int[] sizes, String[] queries) {
    // Write your code here.
}
Checking account…