AlgoMaster Logo
AlgoMasterCompact Adjacent Small Filesmedium

Compact Adjacent Small Files

medium

Data lakes and lakehouses compact small files to reduce listing, planning, and open-file overhead. Files may combine only with adjacent files from the same partition.

Design an AdjacentFileCompactor class:

  • int[] compactedPartitions(int[] partitions, int[] sizes, int targetSize) returns one partition ID per output file.
  • int[] compactedSizes(...) returns the corresponding output sizes.

Scan in input order. Merge the next file into the current group exactly when the partition matches and the combined size is at most targetSize. Otherwise start a new output file. Do not split an individual oversized file or merge across partition boundaries.

Example 1:
Example 2:

Constraints

  • 0 <= partitions.length <= 10^4
  • partitions.length == sizes.length
  • 0 <= partitions[i], sizes[i] <= 10^9
  • 1 <= targetSize <= 10^9
  • Compacted sizes fit in signed 32-bit integers.
Hints

Loading...
CallReturns
new AdjacentFileCompactor()null
compactedPartitions([1,1,1,2,2], [3,4,6,2,7], 10)[1,1,2]
compactedSizes([1,1,1,2,2], [3,4,6,2,7], 10)[7,6,9]

The first two partition-1 files combine to 7, the next remains 6, and both partition-2 files combine to 9.

Run checks these cases. Submit also runs a larger hidden set.