Deduplicate events

Write codeMedium25 minDatabricks and Snowflake style

Your task

You receive a list of event dicts each with keys 'id', 'ts' (int timestamp), and 'status'. Deduplicate so that for each 'id' only the record with the largest 'ts' survives; if two records share the same id and ts, the one appearing later in the input wins. Return the surviving records sorted by 'id' ascending. The input may be empty and ids may repeat any number of times.

Examples

latest_per_id(events: list[dict]) -> list[dict]
Example 1
Inputlatest_per_id([{"id": 1, "ts": 10, "status": "old"}, {"id": 1, "ts": 20, "status": "new"}, {"id": 2, "ts": 5, "status": "x"}])
Output
idtsstatus
120new
25x