KatInterview

Audit Purchase Events Against a Payment Ledger

Arrays & HashingAuto-graded · 16 test cases
MediumAuto-gradedArrays & Hashinghashingaggregationreconciliation

Last updated

Aggregate daily purchase metrics from event logs and reconcile two event snapshots against a ledger to judge whether a tracking fix worked, using hash-map grouping.

A mobile game sells in-app purchases. Its analytics pipeline was broken and then patched. You are given the payment ledger, which is the trusted source of truth, and two snapshots of the purchase events the pipeline emitted: one from before the patch and one from after. Write a function that builds the daily report for each snapshot and checks how well each snapshot agrees with the ledger.

Inputs

All records are dictionaries.

  • daily_active: a list of {"day": str, "user": str}. Each entry says a user was active on a day. The same pair may appear more than once.
  • ledger: a list of {"id": str, "user": str, "day": str, "amount": int}, the real purchases. amount is in cents.
  • events_before: a list of purchase events with the same fields as ledger entries, emitted before the patch.
  • events_after: the same, emitted after the patch.

If several ledger entries share an id, only the last one in list order counts. The ledger therefore holds one purchase per distinct id.

Part 1: Daily metrics

For each snapshot, build a dictionary keyed by day. The set of days included differs between the two reports:

  • "before" report: every day that appears in daily_active or in events_before.
  • "after" report: every day that appears in daily_active, in events_before or in events_after. The after report keeps the whole reporting period, so a day that only had events in the before snapshot still appears, with zero purchases if the after snapshot has none for it.

Days that appear only in the ledger are never included. Each day maps to a dictionary with these keys, computed from that report's own snapshot:

  • dau: the number of distinct users in daily_active for that day (0 if none).
  • reported_purchases: the number of event rows for that day, duplicates included.
  • reported_purchasers: the number of distinct users among that day's event rows.
  • reported_revenue_cents: the sum of amount over that day's event rows.
  • conversion_rate: reported_purchasers / dau, or 0 if dau is 0.
  • arppu_cents: reported_revenue_cents / reported_purchasers, or 0 if there are no purchasers.

These metrics are computed from the events exactly as reported, with no correction against the ledger. Events are grouped by their own day field.

Part 2: Reconciliation

For each snapshot, compare its events with the ledger and compute:

  • actual_purchases: the number of distinct ledger ids.
  • event_rows: the total number of event rows in the snapshot.
  • For each distinct ledger id, let c be the number of event rows carrying that id:
    • c == 0: add 1 to missing_purchases.
    • c == 1: add 1 to matched_events. Then compare that event with the ledger entry. If the amount differs, add 1 to amount_mismatches. If the user or the day differs (or both), add 1 to attribute_mismatches. This is a single increment even if both differ. The two checks are independent, so one event can raise both counters.
    • c > 1: add 1 to matched_events and add c - 1 to duplicate_events. Fields of duplicated events are not compared.
  • extra_events: the number of event rows whose id does not exist in the ledger. Each such row counts, including repeats of the same unknown id.
  • error_score: missing_purchases + extra_events + duplicate_events + amount_mismatches + attribute_mismatches.
  • repair_valid: True exactly when error_score == 0.

Output

Return a dictionary:

{
  "before": <daily metrics for events_before>,
  "after":  <daily metrics for events_after>,
  "validation": {
    "before": <reconciliation dict for events_before>,
    "after":  <reconciliation dict for events_after>,
    "improved": <True if after error_score < before error_score, else False>
  }
}

Each reconciliation dictionary has the keys actual_purchases, event_rows, matched_events, missing_purchases, extra_events, duplicate_events, amount_mismatches, attribute_mismatches, error_score and repair_valid.

Constraints

  • Up to about 10^5 records in each list.
  • amount is a non-negative integer number of cents.
  • Day strings are compared as plain strings. Output days are ordered by ascending string order, which is the same as chronological order for ISO dates.
  • Any list may be empty.

Examples

Example 1

Input: audit_purchase_reporting([{'day': '2025-01-01', 'user': 'u1'}, {'day': '2025-01-01', 'user': 'u2'}, {'day': '2025-01-02', 'user': 'u1'}], [{'id': 't1', 'user': 'u1', 'day': '2025-01-01', 'amount': 499}, {'id': 't2', 'user': 'u2', 'day': '2025-01-01', 'amount': 999}, {'id': 't3', 'user': 'u1', 'day': '2025-01-02', 'amount': 499}], [{'id': 't1', 'user': 'u1', 'day': '2025-01-01', 'amount': 499}, {'id': 't2', 'user': 'u2', 'day': '2025-01-01', 'amount': 999}], [{'id': 't1', 'user': 'u1', 'day': '2025-01-01', 'amount': 499}, {'id': 't2', 'user': 'u2', 'day': '2025-01-01', 'amount': 999}, {'id': 't3', 'user': 'u1', 'day': '2025-01-02', 'amount': 499}])

Output: {'before': {'2025-01-01': {'dau': 2, 'reported_purchases': 2, 'reported_purchasers': 2, 'reported_revenue_cents': 1498, 'conversion_rate': 1, 'arppu_cents': 749}, '2025-01-02': {'dau': 1, 'reported_purchases': 0, 'reported_purchasers': 0, 'reported_revenue_cents': 0, 'conversion_rate': 0, 'arppu_cents': 0}}, 'after': {'2025-01-01': {'dau': 2, 'reported_purchases': 2, 'reported_purchasers': 2, 'reported_revenue_cents': 1498, 'conversion_rate': 1, 'arppu_cents': 749}, '2025-01-02': {'dau': 1, 'reported_purchases': 1, 'reported_purchasers': 1, 'reported_revenue_cents': 499, 'conversion_rate': 1, 'arppu_cents': 499}}, 'validation': {'before': {'actual_purchases': 3, 'event_rows': 2, 'matched_events': 2, 'missing_purchases': 1, 'extra_events': 0, 'duplicate_events': 0, 'amount_mismatches': 0, 'attribute_mismatches': 0, 'error_score': 1, 'repair_valid': False}, 'after': {'actual_purchases': 3, 'event_rows': 3, 'matched_events': 3, 'missing_purchases': 0, 'extra_events': 0, 'duplicate_events': 0, 'amount_mismatches': 0, 'attribute_mismatches': 0, 'error_score': 0, 'repair_valid': True}, 'improved': True}}

Example 2

Input: audit_purchase_reporting([{'day': '2025-02-01', 'user': 'a'}, {'day': '2025-02-01', 'user': 'b'}, {'day': '2025-02-01', 'user': 'b'}], [{'id': 'x', 'user': 'a', 'day': '2025-02-01', 'amount': 100}], [{'id': 'x', 'user': 'a', 'day': '2025-02-01', 'amount': 100}, {'id': 'x', 'user': 'a', 'day': '2025-02-01', 'amount': 100}, {'id': 'bad', 'user': 'b', 'day': '2025-02-01', 'amount': 50}], [{'id': 'x', 'user': 'a', 'day': '2025-02-01', 'amount': 100}])

Output: {'before': {'2025-02-01': {'dau': 2, 'reported_purchases': 3, 'reported_purchasers': 2, 'reported_revenue_cents': 250, 'conversion_rate': 1, 'arppu_cents': 125}}, 'after': {'2025-02-01': {'dau': 2, 'reported_purchases': 1, 'reported_purchasers': 1, 'reported_revenue_cents': 100, 'conversion_rate': 0.5, 'arppu_cents': 100}}, 'validation': {'before': {'actual_purchases': 1, 'event_rows': 3, 'matched_events': 1, 'missing_purchases': 0, 'extra_events': 1, 'duplicate_events': 1, 'amount_mismatches': 0, 'attribute_mismatches': 0, 'error_score': 2, 'repair_valid': False}, 'after': {'actual_purchases': 1, 'event_rows': 1, 'matched_events': 1, 'missing_purchases': 0, 'extra_events': 0, 'duplicate_events': 0, 'amount_mismatches': 0, 'attribute_mismatches': 0, 'error_score': 0, 'repair_valid': True}, 'improved': True}}

Example 3

Input: audit_purchase_reporting([{'day': '2025-03-01', 'user': 'p1'}], [], [], [])

Output: {'before': {'2025-03-01': {'dau': 1, 'reported_purchases': 0, 'reported_purchasers': 0, 'reported_revenue_cents': 0, 'conversion_rate': 0, 'arppu_cents': 0}}, 'after': {'2025-03-01': {'dau': 1, 'reported_purchases': 0, 'reported_purchasers': 0, 'reported_revenue_cents': 0, 'conversion_rate': 0, 'arppu_cents': 0}}, 'validation': {'before': {'actual_purchases': 0, 'event_rows': 0, 'matched_events': 0, 'missing_purchases': 0, 'extra_events': 0, 'duplicate_events': 0, 'amount_mismatches': 0, 'attribute_mismatches': 0, 'error_score': 0, 'repair_valid': True}, 'after': {'actual_purchases': 0, 'event_rows': 0, 'matched_events': 0, 'missing_purchases': 0, 'extra_events': 0, 'duplicate_events': 0, 'amount_mismatches': 0, 'attribute_mismatches': 0, 'error_score': 0, 'repair_valid': True}, 'improved': False}}

Hints

Hint 1

Group by day with dictionaries of sets and lists: one for distinct active users, one for event rows per snapshot. Decide the set of days for each report before computing anything, and remember the after report also covers days seen in the before events.

Hint 2

Index each snapshot by event id, mapping id to the list of rows with that id. Then every ledger id can be classified as missing (0 rows), single (1 row, so compare fields) or duplicated (more than 1 row) in constant time.

Hint 3

Extra events are counted per row, not per distinct id. Duplicate counts only apply to ids that exist in the ledger. Guard both divisions against a zero denominator.