KatInterview

Feature Activation, Time-to-First-Purchase, and Region Switching in SQL

SQL & DataModel answer
MediumModel answerSQL & Datasqlwindow-functionspercentiles

Last updated

Write SQL over users and transactions for regional activation and purchase rates, first-purchase latency with percentiles, and flagging purchases made outside a user's first-purchase region.

A payments app launched a new wallet feature. You have two tables.

users(user_id INT PRIMARY KEY, user_region STRING, adopted_at TIMESTAMP NULL)
transactions(txn_id INT PRIMARY KEY, user_id INT, txn_ts TIMESTAMP, txn_region STRING)

adopted_at is the moment a user turned the feature on, or NULL if they never did.

users

user_iduser_regionadopted_at
1US2025-07-15 10:00:00
2USnull
3EU2025-08-02 09:12:00
4APAC2025-08-20 18:30:00
5EU2025-08-25 12:00:00
6US2025-08-30 23:59:00

transactions

txn_iduser_idtxn_tstxn_region
10112025-08-01 08:00:00US
10212025-08-16 10:00:00CA
10332025-08-10 12:10:00EU
10442025-08-25 07:45:00APAC
10542025-08-26 07:45:00US
10652025-08-30 07:45:00EU
10762025-09-01 00:10:00US

Write ANSI-style SQL for each part. "August" means 2025-08-01 through 2025-08-31 inclusive, covering the whole of the 31st.

A. Regional rates. For each user_region, return region, adoption_rate and transaction_rate.

  • adoption_rate is the number of users whose adopted_at falls in August, divided by all users in that region.
  • transaction_rate is the number of distinct users in that region with at least one transaction in August, divided by all users in that region.
  • Attribute by the user's region, not by txn_region.

B. Time to first transaction.

  1. For each user, compute days_to_first_txn, the number of days from adopted_at to the user's earliest transaction. Include only users with a non-null adopted_at whose earliest transaction is at or after adopted_at. Return user_id and days_to_first_txn.
  2. Write a second query returning the p10, p50 and p90 of days_to_first_txn.

C. Cross-region transactions. A user's first_txn_region is the region of their earliest transaction ever, not just in August. A transaction is cross-region if its txn_region differs from that value. Return every transaction dated in August, with columns txn_id, user_id, txn_ts, txn_region, first_txn_region and is_cross_region.

Explain your choices on date boundaries, day-difference semantics, percentile type and tie-breaking.

Hints

Hint 1

Both rates share the same denominator, the user count per region. Shrink the transactions table to one row per user (DISTINCT user_id within August) before joining, so the join cannot multiply user rows.

Hint 2

Avoid BETWEEN on timestamps with '2025-08-31' as the upper bound, since it cuts off the last day. Use a half-open range: >= '2025-08-01' and < '2025-09-01'.

Hint 3

For part C, compute each user's first region with ROW_NUMBER() over all of their transactions, ordered by txn_ts then txn_id. Apply the August filter only afterwards, in the outer query.

Hint 4

For percentiles, build the per-user latency as a CTE and run PERCENTILE_CONT ... WITHIN GROUP over it. Decide whether you want interpolated or observed values, and say which you chose.