Challenge

Problem

Understanding disk usage is crucial for system administration and application optimization. This drill teaches you to recursively traverse directories, calculate file sizes, filter by file types, and format output for human readability. You'll learn recursive algorithms, directory traversal, file metadata access, and data formattingβ€”essential skills for building DevOps tools, backup systems, and disk usage analyzers.

Difficulty: Intermediate

Instructions

  1. Read target directory path from config.txt
  2. Recursively scan all files and subdirectories
  3. Calculate total size in bytes
  4. Group files by extension and calculate size per extension
  5. Sort extensions by size (largest first)
  6. Format output:
    'DIRECTORY SIZE ANALYSIS'
    'Directory: [path]'
    '=' * 50
    'BY FILE TYPE'
    '.ext: X.XX MB (Y files)' (for each extension, sorted by size)
    'No extension: X.XX MB (Y files)' (if any)
    '-' * 50
    'SUMMARY'
    'Total Files: X'
    'Total Size: X.XX MB'
  7. Format sizes: bytes -> KB -> MB -> GB as appropriate
  8. Handle empty directories gracefully

Files

Editable
Read-only

Hints

Hint 1

Find.find(path) recursively yields every file and directory

Hint 2

File.directory?(path) checks if path is a directory

Hint 3

File.size(path) returns file size in bytes

Hint 4

File.extname(path) returns extension including the dot (e.g., '.txt')

Hint 5

Use 'next' to skip directories in the Find.find loop

Hint 6

Hash.new { |h, k| h[k] = { size: 0, count: 0 } } creates nested default hash

Hint 7

Sort by size descending: array.sort_by { |k, v| -v[:size] }

Hint 8

1024 bytes = 1 KB, 1024 KB = 1 MB, 1024 MB = 1 GB

Provided Files (Read-only)

1. Directory with multiple file types

Input:
analyze_directory('config.txt')
Expected Output:
DIRECTORY SIZE ANALYSIS
Directory: /test_dir
==================================================
BY FILE TYPE
.txt: 2.81 KB (3 files)
.rb: 500 bytes (1 files)
.json: 300 bytes (1 files)
--------------------------------------------------
SUMMARY
Total Files: 5
Total Size: 3.59 KB

2. Large files with MB sizes

Input:
analyze_directory('config.txt')
Expected Output:
DIRECTORY SIZE ANALYSIS
Directory: /large_dir
==================================================
BY FILE TYPE
.mp4: 2.38 MB (1 files)
.jpg: 1.43 MB (1 files)
.pdf: 488.28 KB (1 files)
--------------------------------------------------
SUMMARY
Total Files: 3
Total Size: 4.29 MB

3. Files without extensions

Input:
analyze_directory('config.txt')
Expected Output:
DIRECTORY SIZE ANALYSIS
Directory: /mixed_dir
==================================================
BY FILE TYPE
No extension: 1.46 KB (2 files)
.txt: 300 bytes (1 files)
--------------------------------------------------
SUMMARY
Total Files: 3
Total Size: 1.76 KB
+ 2 hidden test cases