Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Optimize Duplicate Record Aggregation

MediumPython00:00
I
Practice interviewer
Your interviewer
In session
I
Interviewer

Welcome to the Python screen.

The question is on your right: Optimize Duplicate Record Aggregation. Read through the requirements first.

Run and submit your code as often as you need. You also have five interviewer messages this session - want to talk through your approach, or are you ready to start coding?

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