KatInterview

Coupon Campaign Conversion and 48-Hour Spend Report

SQL & DataAuto-graded · 16 test cases
MediumAuto-gradedSQL & Datajoinshash-mapdatetime

Last updated

Build a per-user cohort from coupon events, then join transactions by time window to compute cohort size, converters, and spend within 48 hours.

Coupon Campaign Report

A shop ran a promotion with the coupon code SUMMER20. You are given two logs, each represented as a list of rows, and must summarize how well the promotion performed.

Inputs

  • coupon_events: a list of rows [user_id, coupon_code, applied_at].
    • user_id is an integer.
    • coupon_code is a string.
    • applied_at is an ISO-8601 timestamp string such as "2023-08-01T10:00:00Z".
  • purchases: a list of rows [transaction_id, user_id, amount, transacted_at].
    • transaction_id and user_id are integers.
    • amount is a number.
    • transacted_at is an ISO-8601 timestamp string.

Definitions

  • Cohort: every user who has at least one coupon event with code exactly SUMMER20 and an applied_at in August 2023 (from 2023-08-01 00:00:00 UTC inclusive to 2023-09-01 00:00:00 UTC exclusive).
  • Anchor time: for a cohort user, the earliest qualifying (SUMMER20, August 2023) application time. A user has exactly one anchor, even if they applied the coupon several times.
  • A purchase counts for a cohort user if it belongs to that user and happened strictly after their anchor time.
  • A purchase falls in the 48-hour window if it counts and its time is at most the anchor time plus 48 hours (so a purchase at exactly 48:00:00 after the anchor is included).

Task

Return a dictionary with these three keys:

  • total_coupon_users: the number of users in the cohort.
  • converted_users: the number of cohort users with at least one counting purchase (at any time after the anchor, with no upper limit).
  • total_gmb_48h: the sum of amount over all counting purchases of cohort users that fall in the 48-hour window.

Rules

  • Timestamps ending in Z or carrying an offset must be normalized to UTC before comparing. Timestamps with no timezone are treated as UTC.
  • Coupon events with another code, or outside August 2023, are ignored.
  • Purchases by users outside the cohort are ignored.
  • Purchases at or before the anchor time are ignored for every metric.
  • If the inputs are empty, return 0, 0, and 0.0 respectively.

Constraints

  • Both lists may be empty.
  • Each user may have several coupon events and several purchases.
  • Timestamps are valid ISO-8601 strings.
  • Amounts are non-negative numbers.
  • Input sizes are such that a single pass over each list is expected.

Examples

Example 1

Input: summarize_coupon_campaign([[1, 'SUMMER20', '2023-08-01T10:00:00Z']], [[101, 1, 50, '2023-08-02T09:00:00Z']])

Output: {'total_coupon_users': 1, 'converted_users': 1, 'total_gmb_48h': 50}

Example 2

Input: summarize_coupon_campaign([[1, 'SUMMER20', '2023-08-01T10:00:00Z'], [2, 'OTHER', '2023-08-05T10:00:00Z']], [[101, 1, 50, '2023-08-02T09:00:00Z']])

Output: {'total_coupon_users': 1, 'converted_users': 1, 'total_gmb_48h': 50}

Example 3

Input: summarize_coupon_campaign([[1, 'SUMMER20', '2023-08-15T10:00:00Z'], [2, 'SUMMER20', '2023-08-20T12:00:00Z']], [[101, 1, 10.5, '2023-08-15T11:00:00Z'], [102, 1, 20.25, '2023-08-17T10:00:00Z'], [103, 2, 7, '2023-08-19T12:00:00Z']])

Output: {'total_coupon_users': 2, 'converted_users': 1, 'total_gmb_48h': 30.75}

Hints

Hint 1

Reduce the coupon events to one anchor time per cohort user first: keep only SUMMER20 events in August 2023 and take the earliest per user. This avoids double counting later.

Hint 2

Normalize all timestamps to timezone-aware UTC datetimes before comparing. Handle a trailing 'Z' explicitly and treat naive values as UTC.

Hint 3

For each purchase, look up its user's anchor in a dictionary. Require time > anchor for conversion, and additionally time <= anchor + 48h for the spend total. Track converters in a set.