cswork / libraryOpen practice workspace

Coding questions · Amazon

Transform event logs with subscription windows in pandas

This question evaluates a candidate's competency in time-based event attribution and temporal interval manipulation using pandas, covering interval merging, subscription-aligned revenue aggregation, anomaly flagging, and handling edge cases like overlapping or back-to-back subscriptions.

Using pandas, compute user-level subscription-aligned revenue and anomalies for September 2025. DataFrames: events(user_id:int, ts:UTC datetime, event:str in {'view','add_to_cart','purchase'}, product_id:int, price_usd:float), subs(user_id:int, plan:str, start_ts:UTC datetime, end_ts:UTC datetime or NaT). Requirements: (1) For each user, compute active_subscription_days in 2025-09 and total purchase revenue that occurred while the user was actively subscribed (purchase ts ∈ [start_ts, end_ts)); (2) Flag purchases outside any active window; (3) If a user has overlapping or back-to-back subscriptions, merge them into minimal disjoint half-open intervals before attribution; (4) Output two DataFrames: user_month_agg(user_id, month='2025-09', active_subscription_days:int, subscribed_purchase_revenue:float, out_of_window_purchases:int) and anomalies(user_id, ts, price_usd, reason='outside_window'|'overlap_fixed'); (5) Solve with vectorized operations (e.g., IntervalIndex, merge_asof, or interval trees) and discuss scalability to 100M events with limited RAM (chunking, dtype optimization, parquet scans). Small sample: subs:

 (1,'pro','2025-08-28T00:00Z','2025-09-10T00:00Z')
 (1,'pro','2025-09-10T00:00Z','2025-10-10T00:00Z')
 (2,'basic','2025-09-05T12:00Z',NaT)
events:
 (1,'2025-09-09T22:00Z','purchase',101,19.99)
 (1,'2025-09-15T03:00Z','purchase',102,5.00)
 (2,'2025-09-01T01:00Z','purchase',103,9.99)

Subscription-aligned monthly revenue and anomalies for September 2025

You are given events and subscription intervals for September 2025.

Write one PostgreSQL query that returns a single labeled result table containing both:

  1. user_month_agg: one row per user with active_subscription_days, subscribed_purchase_revenue, and out_of_window_purchases for September 2025.
  2. anomalies: purchases in September that either occurred outside any merged subscription window or were covered by overlapping raw subscription intervals.

Treat subscription intervals as half-open [start_ts, end_ts). NULL end_ts means the subscription continues indefinitely. Merge overlapping or adjacent intervals per user before counting active days and classifying in-window purchases. Return all rows in one output with a result_set column. Columns that do not apply to a result_set should be NULL.

Table subs

columntypeconstraints
user_idINTNOT NULL
planVARCHAR(20)NOT NULL
start_tsTIMESTAMPNOT NULL
end_tsTIMESTAMP

Sample data:

end_tsplanstart_tsuser_id
2025-09-10 00:00:00pro2025-08-28 00:00:001
2025-10-10 00:00:00pro2025-09-10 00:00:001
NULLbasic2025-09-05 12:00:002
2025-09-10 00:00:00basic2025-08-30 00:00:003
2025-09-20 00:00:00pro2025-09-05 00:00:003

Table events

columntypeconstraints
user_idINTNOT NULL
tsTIMESTAMPNOT NULL
eventVARCHAR(20)NOT NULL
product_idINT
price_usdDECIMAL(10,2)

Sample data:

eventprice_usdproduct_idtsuser_id
purchase19.991012025-09-09 22:00:001
purchase51022025-09-15 03:00:001
view19.991012025-08-20 12:00:001
purchase9.991032025-09-01 01:00:002
purchase4.51042025-09-06 10:00:002
view4.51042025-09-10 09:00:002
purchase121062025-09-03 08:00:003
purchase201072025-09-07 12:00:003
purchase81082025-09-25 18:00:003
purchase71052025-08-29 09:00:003
purchase151092025-09-10 11:00:004
view151102025-09-20 09:30:004

Expected output

Sample output for the single labeled result table.

active_subscription_daysmonthout_of_window_purchasesprice_usdreasonresult_setsubscribed_purchase_revenuetsuser_id
302025-090NULLNULLuser_month_agg24.99NULL1
262025-091NULLNULLuser_month_agg4.50NULL2
192025-091NULLNULLuser_month_agg32.00NULL3
02025-091NULLNULLuser_month_agg0NULL4
NULLNULLNULL9.99outside_windowanomaliesNULL2025-09-01 01:00:002
NULLNULLNULL20.00overlap_fixedanomaliesNULL2025-09-07 12:00:003
NULLNULLNULL8.00outside_windowanomaliesNULL2025-09-25 18:00:003
NULLNULLNULL15.00outside_windowanomaliesNULL2025-09-10 11:00:004

Hints

  1. Merge intervals with a running max of prior end_ts.
  2. Use raw interval overlap counts to detect overlap anomalies after normalization.
  3. Union aggregate rows and anomaly rows into one result table.