You’re on the fraud and reliability team at a high-volume fintech processor that ingests tens of millions of card transactions per day. Due to retries, network partitions, and at-least-once delivery, the same transaction log can be recorded multiple times. Duplicates inflate revenue reporting, trigger false fraud alerts, and create audit issues.
You are given a list of transaction log entries. Each entry is a dictionary with:
tx_id (string): globally unique transaction identifiertimestamp (int): Unix epoch seconds when the log was writtenamount_cents (int): transaction amount in centsA duplicate is defined as an entry with the same tx_id as another entry. When duplicates exist for the same tx_id, you must keep exactly one entry using the following rules:
timestamp.amount_cents.Return the deduplicated logs as a list of entries sorted by timestamp ascending, and for ties by tx_id ascending.
Implement dedupe_transaction_logs(logs).
Example 1
logs = [ {"tx_id":"A", "timestamp":100, "amount_cents":500}, {"tx_id":"B", "timestamp":101, "amount_cents":700}, {"tx_id":"A", "timestamp": 99, "amount_cents":500} ][ {"tx_id":"A", "timestamp": 99, "amount_cents":500}, {"tx_id":"B", "timestamp":101, "amount_cents":700} ]tx_id="A" appears twice; keep the earliest timestamp (99). Then sort by timestamp.Example 2
logs = [ {"tx_id":"X", "timestamp":200, "amount_cents":1000}, {"tx_id":"X", "timestamp":200, "amount_cents": 999}, {"tx_id":"Y", "timestamp":199, "amount_cents": 500} ][ {"tx_id":"Y", "timestamp":199, "amount_cents":500}, {"tx_id":"X", "timestamp":200, "amount_cents": 999} ]X, timestamps tie, so keep smaller amount (999).1 <= logs.length <= 2 * 10^51 <= len(tx_id) <= 640 <= timestamp <= 2 * 10^9-10^9 <= amount_cents <= 10^9logs = [{"tx_id":"A","timestamp":100,"amount_cents":500},{"tx_id":"B","timestamp":101,"amount_cents":700},{"tx_id":"A","timestamp":99,"amount_cents":500}]Output[{"tx_id":"A","timestamp":99,"amount_cents":500},{"tx_id":"B","timestamp":101,"amount_cents":700}]WhyTwo entries share tx_id A; keep the one with timestamp 99. Then sort by timestamp.logs = [{"tx_id":"X","timestamp":200,"amount_cents":1000},{"tx_id":"X","timestamp":200,"amount_cents":999},{"tx_id":"Y","timestamp":199,"amount_cents":500}]Output[{"tx_id":"Y","timestamp":199,"amount_cents":500},{"tx_id":"X","timestamp":200,"amount_cents":999}]WhyFor X, timestamps tie so pick smaller amount (999). Output is sorted by timestamp, then tx_id.1 <= logs.length <= 2 * 10^51 <= len(tx_id) <= 640 <= timestamp <= 2 * 10^9-10^9 <= amount_cents <= 10^9def dedupe_transaction_logs(logs: list[dict]) -> list[dict]: