Upsert by version
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.
Implement
dedupe_upsert(records: list[list]) → list[list]Examples
in
[[["a",1,"x"],["a",2,"y"],["b",1,"z"]]]out[["a",2,"y"],["b",1,"z"]]What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 24 min
solution.py
InputExpectedGot
[[["a",1,"x"],["a",2,"y"],["b",1,"z"]]][["a",2,"y"],["b",1,"z"]]not run yetsample