Folder weigh-in
EasyOnline Assessment
Report how much space each folder of a backup drive takes up, counting everything inside it at any depth.
The drive's file list has one string "path size" per file: a path of folder names and a file name joined by / (for example "src/app/main.py"), one space, and the size in bytes. Folder names never contain dots, and file names always do, so no path is both a file and a folder.
A file counts toward every folder above it: src/app/main.py adds its size to src/app and to src. Files directly in the root (no / in the path) belong to no folder.
If the same path is listed more than once, the file was saved again and only its last listing counts.
A folder is reported only if its total is greater than 0 and it sits at most depth levels down (src is level 1, src/app is level 2). Deeper files still count toward their reported ancestors.
Input: files, a list of strings in the format above, and depth, an integer of at least 1.
Output: a list of strings "folder total" with the folder path written without a trailing slash. Sort by total descending; equal totals are ordered by folder path in ordinary ascending string order. If no folder qualifies, return [].
Example 1
- Input:
- [["src/app/main.py 300","src/app/util.py 120","src/lib/io.py 80","README.md 40","docs/guide.md 500"],2]
- Output:
- ["docs 500","src 500","src/app 420","src/lib 80"]
- Explanation
- main.py (300) and util.py (120) make src/app 420, io.py gives src/lib 80, so src totals 500. README.md sits in the root and belongs to no folder. docs is 500 too, and the tie with src is broken by path: docs first. Both levels are within depth 2.
Example 2
- Input:
- [["a/x.txt 10","a/b/y.txt 5","a/x.txt 30","c/z.txt 0","c/d/w.txt 0"],1]
- Output:
- ["a 35"]
- Explanation
- a/x.txt is listed twice and only the last listing (30) counts, so a is 30 + 5 = 35. The folder c holds only empty files, so its total is 0 and it is left out. With depth 1 the subfolder a/b is not reported although its 5 bytes still count toward a.
Example 3
- Input:
- [["logs/2024/jan/a.log 70","logs/2024/feb/b.log 30","logs/old.log 5","tmp/x.tmp 105"],3]
- Output:
- ["logs 105","tmp 105","logs/2024 100","logs/2024/jan 70","logs/2024/feb 30"]
- Explanation
- Everything under logs adds up through every level: logs is 70 + 30 + 5 = 105, logs/2024 is 100. With depth 3 the month folders jan and feb are reported too. tmp is also 105, and logs comes before tmp on the tie.
Constraints
0 <= files.length <= 2,0001 <= depth <= 10- Paths use letters, digits,
_and-in names with/between them; file names contain a dot, folder names do not; paths are at most 10 levels deep 0 <= size <= 1,000,000,000