Your question is Optimize Duplicate Record Aggregation. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
At Acme Analytics, a Python batch job is timing out while processing a large list of event records. Each record is represented as a pair [record_id, value]. Multiple records may share the same record_id. Your task is to optimize the job by aggregating all values for the same record_id and returning the result sorted by record_id.
A naive solution scans the full list repeatedly for each ID, which is too slow on large datasets. Write an efficient function that processes the list in near-linear time.
records, a list of pairs [record_id, value], where both elements are integers.[record_id, total_value] such that each record_id appears once and total_value is the sum of all values associated with that ID, sorted in ascending order by record_id.def aggregate_records(records):