Given two CSV files of transaction start and complete events (transaction ID, user, timestamp), calculate each transaction duration, accounting for starts without a completion and duplicate transactions for the same user.
Asked in the phone screen stage. OP states the same question was given at their phone screen.
Implement calculate_transaction_durations(starts, completes). Each argument is a list of dictionaries with transaction_id, user, and integer timestamp. Rows in each list are chronological. Match completions to the earliest unmatched start with the same transaction ID and user. Return one dictionary per start, preserving start order, with transaction_id, user, start_timestamp, complete_timestamp, and duration. Use None for both completion and duration when no completion exists. Ignore completions without a matching start.
Examples: [{"transaction_id":"t1","user":"u1","timestamp":10}] and [{"transaction_id":"t1","user":"u1","timestamp":25}] produce duration 15. Two starts for the same key and one completion leave the second start unmatched.
Constraints: At most 100,000 rows per file; timestamps are nonnegative integers.
def calculate_transaction_durations(starts, completes):