Disk Space Calculator

Medium2s max execution

You're working on a file system implementation and need to calculate how much disk space a file actually occupies when stored on disk.

📀 Understanding File Storage:

  1. Files have two different sizes to consider:
    • The actual file size in octets/bytes
    • The disk space used, which is always greater than or equal to the file size
  2. Storage organization:
    • Storage is divided into fixed-size "clusters"
    • A file can use multiple clusters if needed
    • Each cluster can only be used by one file
    • The total disk space used is the sum of all clusters allocated to the file
  3. Important rules:
    • You must allocate whole clusters (no partial clusters allowed)
    • If a file needs even slightly more than N clusters, you must allocate N+1 clusters
    • Empty space in the last cluster is wasted

💡 Quick Example:

For a file of 1500 bytes on a disk with 512-byte clusters:

  • 1500 ÷ 512 = 2.93 clusters needed
  • We must round up to 3 complete clusters
  • Total space used = 3 × 512 = 1536 bytes

🎯 Your Task:

Complete the computeSizeOnDisk method that calculates the actual disk space used by a file, given:

  • The size of each cluster (in octets/bytes)
  • The size of the file (in octets/bytes)

Examples:

Input: [512, 1500]
Output: 1536

1500 bytes needs 3 complete clusters (2.93 rounded up to 3), so 3 × 512 = 1536 bytes total

Input: [1024, 2048]
Output: 2048

2048 bytes fits perfectly in 2 clusters of 1024 bytes (no wasted space)

Input: [512, 513]
Output: 1024

513 bytes needs 2 clusters because 1 cluster (512 bytes) isn't enough

Constraints:

  • •1 ≤ clusterSize ≤ 10000
  • •1 ≤ fileSize ≤ 100000
  • •All inputs are positive integers
  • •All values are in octets (bytes)
TypeScript solution with full type checking
Loading...

Custom Test Input

Output

Run your code to see output...