AlgoMaster Logo
AlgoMasterCompact Multiple SSTableshard

Compact Multiple SSTables

hard

Compaction commonly merges more than two SSTables. A key may have several historical values, but only the value from the newest segment can survive. A newest tombstone deletes the key entirely.

Design an NWaySSTableCompactor class:

  • NWaySSTableCompactor() creates a stateless compactor.
  • int[][] compact(int[][] entries) returns the resolved live rows sorted by key.

Each input row is [segmentIndex, key, value]. A larger segmentIndex is newer. For each key, keep the row with the largest segment index. If its value is -1, omit the key; otherwise return [key, value].

Example 1:

Input:

Output:

Explanation: For key 3, segment 1 is newer than segment 0, so value 99 wins.

Example 2:

Input:

Output:

Explanation: Key 5 has a newer tombstone, so the older live value cannot survive.

Constraints

  • 0 <= entries.length <= 10^5
  • entries[i].length == 3
  • 0 <= segmentIndex <= 10^9
  • Each (segmentIndex, key) pair occurs at most once.
  • A value of -1 is a tombstone; all other keys and values fit in signed 32-bit integers.
  • At most 100 calls are made to compact.
Hints

Loading...
CallReturns
new NWaySSTableCompactor()null
compact([[0,1,10],[0,3,30],[1,3,99],[2,4,40]])[[1,10],[3,99],[4,40]]

Segment 1 is newer than segment 0 for key 3, so value 99 wins. Keys 1 and 4 each have one version.

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