Upsert by version

Write codeMedium24 minDatabricks and Snowflake style

Your task

Implement an upsert/merge that collapses a change stream into the latest state per key. Given a list of change records [key, version, value] (key string, version int, value string), produce the final table keeping, for each key, the record with the HIGHEST version. If two records share a key and the same highest version, keep the one that appears LATER in the input list (last-write-wins tie-break). Return [key, version, value] rows sorted by key ascending. Up to 50000 records.

Examples

dedupe_upsert(records: list[list]) -> list[list]
Example 1
Inputdedupe_upsert([["a", 1, "x"], ["a", 2, "y"], ["b", 1, "z"]])
Output[["a", 2, "y"], ["b", 1, "z"]]