Disk usage roll-up
A free medium coding practice problem from sysadmin. Solve it in JavaScript, TypeScript or Python and check your solution against fresh hidden tests — edge cases and random inputs — as often as you like.
- Difficulty
- Medium
- Type
- Function
- Topic
- Sysadmin
- Languages
- JavaScript, TypeScript or Python
Problem
files lists [path, bytes] for files (absolute paths like "/var/log/syslog", components separated by single /, no trailing slash). Roll the sizes up into directories at most depth levels deep (depth ≥ 1): a file's directory is its path without the last component; keep only the first depth components of that directory. So with depth = 1, /var/log/syslog counts towards /var; with depth = 2, towards /var/log; /etc/hosts counts towards /etc for any depth; a file directly under the root (/vmlinuz) counts towards /.
Return [[dir, totalBytes], …] sorted by total DESCENDING, ties by dir ascending (character-code order). Empty input → []. Up to 100 000 files.
Function signature
// JavaScript
function diskUsageRollup(files, depth)
// TypeScript
function diskUsageRollup(files: any, depth: any): any
// Python
def disk_usage_rollup(files, depth):Examples
diskUsageRollup([["/var/log/syslog",500],["/var/log/auth.log",100],["/var/lib/db",1000],["/etc/hosts",1]], 1)→[["/var",1600],["/etc",1]]diskUsageRollup([["/var/log/syslog",500],["/var/log/auth.log",100],["/var/lib/db",1000],["/etc/hosts",1]], 2)→[["/var/lib",1000],["/var/log",600],["/etc",1]]
How checking works
Run your code against the examples above, then submit a check: it runs on the server against hidden tests built fresh each time, including edge cases and random inputs. Practice is untimed and never affects a verified score; when you are ready, take a graded coding challenge to earn a badge.