cswork / libraryOpen practice workspace

Coding questions · Coinbase

Calculate Adoption and Transaction Rates, Identify Cross-Region Sales

This question evaluates data manipulation and analytics skills in SQL/Python, focusing on aggregation, event-time calculations, and identifying cross-region transaction behavior to compute adoption and transaction rates.

user_txn

+---------+-------------+------------+---------------+------------------+---------------------+
| user_id | user_region | adopted_at | transacted_at  | transacted_region | timestamp           |
+---------+-------------+------------+---------------+------------------+---------------------+
| 101     | US          | 2023-01-05 | 2023-01-10    | US               | 2023-01-10 09:00    |
| 101     | US          | 2023-01-05 | 2023-02-01    | CA               | 2023-02-01 12:30    |
| 202     | CA          | 2023-01-07 | 2023-01-20    | CA               | 2023-01-20 14:00    |
| 303     | UK          | 2023-01-08 | NULL          | NULL             | NULL                |
| 404     | US          | 2023-01-09 | 2023-03-01    | UK               | 2023-03-01 16:45    |
+---------+-------------+------------+---------------+------------------+---------------------+
Scenario

A user_txn table records user adoption dates and subsequent transactions across regions; the business wants adoption/transaction KPIs and insights on cross-region behavior.

Question

Compute overall adoption_rate (users with adopted_at) and transaction_rate (users with at least one transaction) for a given date range. For each adopted user, calculate the time in days from adoption to their first transaction. Identify cross-region sales: transactions where transacted_region differs from the region of the user’s first transaction, and list those transactions.

Hints

Use conditional aggregation for rates, MIN() OVER or subqueries for first transaction, and compare regions in a CTE.

Adoption and transaction KPIs with cross-region behavior

Using the user_txn table for the date range 2023-01-01 to 2023-03-31, return one combined result set with columns result_set, start_date, end_date, base_users, adopted_users, users_with_txn, adoption_rate, transaction_rate, user_id, user_region, adopted_at, first_txn_ts, first_txn_region, days_to_first_txn, transacted_at, transacted_region, and txn_ts.

  1. result_set = 'metrics': one KPI row with base_users, adopted_users, users_with_txn, adoption_rate, and transaction_rate.
  2. result_set = 'user_first_txn': one row per user whose adopted_at is in the date range, including first transaction timestamp/region and days_to_first_txn. Users with no transaction should have NULL first-transaction fields.
  3. result_set = 'cross_region_txns': transactions in the date range where transacted_region differs from that user's first transaction region.

Fields that do not apply to a row should be NULL.

Table user_txn

columntypeconstraints
user_idINTEGERNOT NULL
user_regionVARCHAR(2)NOT NULL
adopted_atDATENOT NULL
transacted_atDATE
transacted_regionVARCHAR(2)
timestampTIMESTAMP

Sample data:

adopted_attimestamptransacted_attransacted_regionuser_iduser_region
2023-01-052023-01-10 09:002023-01-10US101US
2023-01-052023-02-01 12:302023-02-01CA101US
2023-01-072023-01-20 14:002023-01-20CA202CA
2023-01-08NULLNULLNULL303UK
2023-01-092023-03-01 16:452023-03-01UK404US

Expected output

One combined result set for KPI metrics, per-user first-transaction details, and cross-region transactions in the 2023-01-01 to 2023-03-31 window.

adopted_atadopted_usersadoption_ratebase_usersdays_to_first_txnend_datefirst_txn_regionfirst_txn_tsresult_setstart_datetransacted_attransacted_regiontransaction_ratetxn_tsuser_iduser_regionusers_with_txn
NULL414NULL2023-03-31NULLNULLmetrics2023-01-01NULLNULL0.75NULLNULLNULL3
2023-01-05NULLNULLNULL5NULLUS2023-01-10 09:00user_first_txnNULLNULLNULLNULLNULL101USNULL
2023-01-07NULLNULLNULL13NULLCA2023-01-20 14:00user_first_txnNULLNULLNULLNULLNULL202CANULL
2023-01-08NULLNULLNULLNULLNULLNULLNULLuser_first_txnNULLNULLNULLNULLNULL303UKNULL
2023-01-09NULLNULLNULL51NULLUK2023-03-01 16:45user_first_txnNULLNULLNULLNULLNULL404USNULL
2023-01-05NULLNULLNULLNULLNULLUS2023-01-10 09:00cross_region_txnsNULL2023-02-01CANULL2023-02-01 12:30101USNULL

Hints

  1. Build the shared adoption and first-transaction CTEs once.
  2. Use the first transaction region as the baseline for cross-region detection.
  3. Use typed NULLs when combining different result shapes.