LSM compaction with tombstones

Write codeMedium25 minMeta and Databricks style

Your task

Compact an LSM key-value log by dropping tombstones. You are given a list of records in chronological order; each is [key, value] for a write, or [key, null] (a tombstone marking a delete), in JSON null is None. For each key, only its LAST record matters. After applying last-writer-wins, a key whose final record is a tombstone is fully removed (its tombstone is dropped during compaction, not retained). Return the compacted live data as a list of [key, value] pairs sorted ascending by key, containing only keys whose final state is a live value. Keys are strings; <= 10^5 records.

Examples

compact_tombstones(records: list[list]) -> list[list]
Example 1
Inputcompact_tombstones([["a", 1], ["b", 2], ["a", None]])
Output[["b", 2]]