cswork / libraryOpen practice workspace

Coding questions · PayPal

Clean and Summarize User Purchase Data Efficiently

This question evaluates proficiency in data cleaning, aggregation, and time-series manipulation within the Data Manipulation (SQL/Python) domain, focusing on filtering by group counts and calculating inter-event timing metrics.

transactions

+-----------+---------------------+-----------+--------+
| user_id   | txn_timestamp       | txn_value | txn_id |
+-----------+---------------------+-----------+--------+
| 201       | 2023-09-01 09:00:00 | 15.50     | 1      |
| 201       | 2023-09-01 09:05:00 | 17.00     | 2      |
| 202       | 2023-09-01 10:00:00 | 20.00     | 3      |
| 201       | 2023-09-02 11:00:00 | 5.00      | 4      |
| 203       | 2023-09-02 11:05:00 | 22.00     | 5      |
+-----------+---------------------+-----------+--------+
Scenario

You have a pandas DataFrame with user purchase history and need to clean and summarize it for analysts.

Question

a) Implement a function that removes every user who has fewer than 100 transactions in any calendar month. b) Implement a second function that returns the average time between consecutive transactions, in seconds, for each remaining user.

Hints

Group by user & month; use shift to compute timedeltas; convert Timedelta to seconds via .dt.total_seconds().

Filter users by monthly transaction count and compute average time between transactions

You are given a transactions table with user purchase history. Write a single SQL query that:

a) Removes every user who has fewer than 100 transactions in any calendar month.

b) For the remaining users, returns the average time between consecutive transactions, in seconds, for each user.

Return one row per remaining user, with their user_id and the average number of seconds between their consecutive transactions.

Table transactions

columntypeconstraints
user_idINTEGERNOT NULL
txn_timestampTIMESTAMPNOT NULL
txn_valueDECIMAL(10,2)NOT NULL
txn_idINTEGERPRIMARY KEY, NOT NULL

Sample data:

txn_idtxn_timestamptxn_valueuser_id
12023-09-01 09:00:0015.5201
22023-09-01 09:05:0017201
32023-09-01 10:00:0020202
42023-09-02 11:00:005201
52023-09-02 11:05:0022203

Expected output

One row per remaining user with the average number of seconds between their consecutive transactions. Given the sample data, every user has a month with fewer than 100 transactions, so all users are disqualified and the result set is empty.

Hints

  1. First, aggregate by user_id and calendar month (e.g., using date_trunc('month', txn_timestamp)) to count monthly transactions.
  2. Disqualify any user who has at least one month with a transaction count less than 100.
  3. Use LAG over (PARTITION BY user_id ORDER BY txn_timestamp) to access each user's previous transaction timestamp.
  4. Compute the time difference in seconds with EXTRACT(EPOCH FROM (current_ts - previous_ts)) and then average these per user.