cswork / libraryOpen practice workspace

Coding questions · DoorDash

Generate Weekly Revenue and Engagement Summary with Pandas

This question evaluates proficiency in data manipulation and analytics with Pandas and SQL, covering aggregations, distinct purchase counts, lambda-based user-tier classification, and platform-level conversion-rate computation.

events

user_idevent_timeevent_typeplatformrevenue
1012024-04-01 12:01:00clickiOS0.00
1012024-04-01 12:02:10purchaseiOS4.99
2022024-04-01 13:05:33viewAndroid0.00
3032024-04-01 14:20:05clickWeb0.00
2022024-04-01 14:45:00purchaseAndroid9.99
Scenario

You own the clickstream pipeline for a consumer app and must create a weekly revenue and engagement summary.

Question

Using Pandas, create a DataFrame that aggregates total revenue and distinct purchase counts per user from the events table. Use a lambda function inside apply to classify users into tiers based on total revenue (e.g., 0, 0–5, 5+). Write a SQL query that returns, for each platform, the daily conversion rate (purchases / clicks) for the last 30 days.

Hints

Show familiarity with groupby, apply-lambda, dictionary mapping, and SQL aggregations with conditional filtering.

User Revenue And Purchase Tiers

For each user in the events table, compute total_revenue (sum of revenue from purchase events only), purchase_count (number of purchase events), and revenue_tier: 'low' if total_revenue < 5, 'medium' if 5 <= total_revenue < 10, and 'high' if total_revenue >= 10. Return one row per user.

Table events

columntypeconstraints
user_idINTEGER
event_timeTIMESTAMP
event_typeVARCHAR(20)
platformVARCHAR(20)
revenueDECIMAL(10,2)

Sample data:

event_timeevent_typeplatformrevenueuser_id
2025-05-10 12:01:00clickiOS0101
2025-05-10 12:02:10purchaseiOS4.99101
2025-05-10 13:05:33viewAndroid0202
2025-05-10 14:20:05clickWeb0303
2025-05-10 14:45:00purchaseAndroid9.99202

Expected output

One row per user with total purchase revenue, number of purchase events, and a revenue tier classification.

purchase_countrevenue_tiertotal_revenueuser_id
1low4.99101
1medium9.99202
0low0303

Hints

  1. Sum revenue only for rows where event_type = 'purchase'.
  2. Use a CASE expression on the aggregated total_revenue to assign the revenue_tier.

Daily Platform Conversion Rate

For each platform and calendar day between '2025-05-03' and '2025-06-01' (inclusive), return purchases (count of purchase events), clicks (count of click events), and conversion_rate = purchases / clicks (NULL if clicks = 0). Order by event_date and platform.

Table events

columntypeconstraints
user_idINTEGER
event_timeTIMESTAMP
event_typeVARCHAR(20)
platformVARCHAR(20)
revenueDECIMAL(10,2)

Sample data:

event_timeevent_typeplatformrevenueuser_id
2025-05-10 12:01:00clickiOS0101
2025-05-10 12:02:10purchaseiOS4.99101
2025-05-10 13:05:33viewAndroid0202
2025-05-10 14:20:05clickWeb0303
2025-05-10 14:45:00purchaseAndroid9.99202

Expected output

For each platform and day between '2025-05-03' and '2025-06-01', show purchases, clicks, and conversion_rate. Based on the sample rows, for 2025-05-10 the results are:

clicksconversion_rateevent_dateplatformpurchases
0NULL2025-05-10Android1
112025-05-10iOS1
102025-05-10Web0

Hints

  1. Group by DATE(event_time) and platform to get daily counts per platform.
  2. Use conditional SUM(CASE WHEN ...) to count purchases and clicks, and NULLIF in the denominator to avoid division by zero when computing conversion_rate.