Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Clean and Summarize Event Records

Easy
EasyCodingHash TablesArraysSorting

Problem

A data team at Acme Analytics receives raw event records as Python dictionaries. Implement a function that cleans these records and returns a per-user summary suitable for analysis.

Given a list of event dictionaries, process each record as follows:

  1. Ignore records missing user_id, event, or value.
  2. Normalize event by trimming whitespace and converting to lowercase.
  3. Keep only records where normalized event is one of "click", "view", or "purchase".
  4. Convert value to an integer if possible; otherwise ignore the record.
  5. Aggregate valid records by user_id.

For each user, return:

  • user_id
  • total_value: sum of valid values
  • event_counts: counts for each valid event type that appeared

Return the final result as a list of dictionaries sorted by:

  1. total_value descending
  2. user_id ascending

Constraints

  • 1 <= len(records) <= 10^4
  • Each record is a dictionary with string keys
  • event may contain extra whitespace or mixed case
  • value may be an integer or a numeric string
  • Only click, view, and purchase are valid events

Function Signature

def summarize_records(records):
Take this as a live interview session →

You are practicing as a guest. Sign up free to run your code against the sample data. Your draft stays right here.

Sign up freeI have an account
def solve(rows):
    counts = {}
    for row in rows:
        ...
    return result
Sign up to unlock solutions
Next questions
Clean Dataset RecordsEasyMicro1Optimize Pandas Pipeline BottlenecksMediumSummarize Python Sales RecordsEasy