KatInterview

Collapse Rapid Cart Adds into Daily Pacific Action Counts

Arrays & HashingAuto-graded · 15 test cases
MediumAuto-gradedArrays & Hashinghashinggroupingdatetime

Last updated

Filter add-to-cart events, convert UTC to Pacific dates, merge rapid repeats into single actions with a chained gap rule, and count the actions per user, date and SKU.

Collapse Rapid Cart Adds into Daily Pacific Action Counts

You are given a raw clickstream log as a list of event dictionaries. Some events are repeated taps on the same "add to cart" button. Your job is to merge those bursts into single logical actions and report how many actions each user took per product per Pacific calendar day.

Input

event_log is a list of dictionaries. Each dictionary has these keys:

  • user_id: identifier of the user (int or string).
  • event_time_utc: an ISO-8601 timestamp string, such as "2025-08-30 16:00:00". A string with no offset is in UTC. A string ending in Z or carrying an explicit offset must be converted to the correct instant.
  • event_type: string, such as "add_to_cart" or "purchase_click".
  • session_id: session identifier (may be missing or None).
  • props_json: extra properties, given either as a dictionary or as a JSON-encoded string of a dictionary. It may also be None or empty. The product code is under the key "sku". If the key is absent, the SKU is None.

Task

  1. Ignore every event whose event_type is not "add_to_cart".
  2. For each remaining event, read its sku and compute its local date: the calendar date of event_time_utc in the America/Los_Angeles time zone, formatted YYYY-MM-DD. Daylight saving time must be handled correctly. Convert the instant. Do not just relabel the clock time.
  3. Group the events by (user_id, session_id, sku) and sort each group by time. Within a group, an event starts a new action if it is the first event in the group, or if its gap to the immediately preceding event in that group is strictly greater than 2 minutes. Otherwise it belongs to the same action as the previous event.
    • The rule chains: events at 0:00, 1:30 and 3:00 are all one action.
    • A gap of exactly 2 minutes still belongs to the same action.
  4. Each action is attributed to the local date of the event that starts it.
  5. Count actions per (user_id, local_date, sku). Counts from different sessions are added together.

Output

Return a list of dictionaries, each with exactly these keys:

  • user_id
  • local_dt: the Pacific date string.
  • sku
  • dedup_add_to_cart_cnt: the number of actions (an integer, at least 1).

Include only combinations that have at least one action. Sort the list by str(user_id), then local_dt, then str(sku), all ascending. If there are no add-to-cart events, return an empty list.

Constraints

  • 0 <= len(event_log) <= 10^5
  • Timestamps are valid ISO-8601 strings. Naive ones are UTC.
  • Gaps are measured on the real time line, not on local clock times.
  • props_json is either a dict, a JSON string of a dict, or None.
  • Events may arrive in any order.

Examples

Example 1

Input: summarize_cart_actions([{'user_id': 1, 'event_time_utc': '2025-08-30 16:00:00', 'event_type': 'add_to_cart', 'session_id': 's1', 'props_json': {'sku': 'A1', 'qty': 1}}, {'user_id': 1, 'event_time_utc': '2025-08-30 16:01:00', 'event_type': 'add_to_cart', 'session_id': 's1', 'props_json': {'sku': 'A1', 'qty': 1}}, {'user_id': 1, 'event_time_utc': '2025-08-30 16:05:00', 'event_type': 'purchase_click', 'session_id': 's1', 'props_json': {}}, {'user_id': 2, 'event_time_utc': '2025-09-01 01:02:03', 'event_type': 'add_to_cart', 'session_id': 's2', 'props_json': {'sku': 'B2', 'qty': 2}}, {'user_id': 3, 'event_time_utc': '2025-08-25 09:10:00', 'event_type': 'add_to_cart', 'session_id': 's3', 'props_json': {'sku': 'C3', 'qty': 1}}])

Output: [{'user_id': 1, 'local_dt': '2025-08-30', 'sku': 'A1', 'dedup_add_to_cart_cnt': 1}, {'user_id': 2, 'local_dt': '2025-08-31', 'sku': 'B2', 'dedup_add_to_cart_cnt': 1}, {'user_id': 3, 'local_dt': '2025-08-25', 'sku': 'C3', 'dedup_add_to_cart_cnt': 1}]

Hints

Hint 1

Filter first, then build a dictionary that maps (user_id, session_id, sku) to a list of (timestamp, local_date) pairs.

Hint 2

Sort each list by timestamp and compare every event only with the one right before it. Start a new action when there is no predecessor or the gap is strictly greater than 2 minutes. Update the 'previous' time after every event, even duplicates, so chains work.

Hint 3

Use zoneinfo to convert the aware UTC instant to America/Los_Angeles before taking .date(). Then tally actions in a second dictionary keyed by (user_id, local_date, sku) and sort the output by the string forms of user_id and sku.