Code Room
CodingMediumcod-g1165
Subject Database queryLevel Mid–Senior~24 minCommon in Databases & SQL interviewsIndustries Software development

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.