Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Optimize Duplicate Record Aggregation

EasyPython00:00
Practice interviewer
In session
5 left
00:00

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.

You need to log in / sign up to run or submit.

Problem

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.

Formal Specification

  • Input: records, a list of pairs [record_id, value], where both elements are integers.
  • Output: A list of pairs [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.

Constraints

  • 1 <= len(records) <= 2 * 10^5
  • 0 <= record_id <= 10^9
  • -10^9 <= value <= 10^9
  • Each record contains exactly two integers

Function Signature

def aggregate_records(records):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output