cswork / libraryOpen practice workspace

Coding questions · Chime

Compute early signals for user retention

This question evaluates the ability to compute cohort-based early engagement metrics and Day-30 retention using time-windowed joins, aggregation, bucketing, and handling edge cases such as users with zero or pre-signup transactions.

You are given two tables that track signups and user transactions.

Tables

users

  • user_id (STRING, PK) — unique user
  • signup_ts (TIMESTAMP) — time the user signed up (assume UTC)

transactions

  • transaction_id (STRING, PK)
  • user_id (STRING, FK → users.user_id)
  • transaction_ts (TIMESTAMP) — time of transaction (UTC)
  • amount (NUMERIC) — transaction amount

Task

You want an early signal that predicts Day-30 retention.

  1. Define an early engagement metric as the number of transactions a user makes in the first 7 days after signup:
  • early_txn_cnt = count of transactions where transaction_ts >= signup_ts and transaction_ts < signup_ts + 7 days.
  1. Define Day-30 retention as whether the user has at least one transaction in the window [signup + 30 days, signup + 37 days).
  • is_retained_d30 ∈ {0,1}
  1. Write a SQL query that outputs retention performance by early-engagement bucket:
  • Bucket early_txn_cnt into: 0, 1, 2, 3+

Required output

Return one row per bucket with:

  • early_txn_bucket
  • users_in_bucket
  • retained_users
  • retention_rate (= retained_users / users_in_bucket, as a decimal)

Notes / assumptions

  • Include users with zero transactions.
  • If a user has transactions before signup_ts, ignore them.
  • If multiple transactions occur in the retention window, the user still counts as retained once.

Early-activity signals and retention rate by bucket

You are given two tables: users and transactions.

Define an "early signal" of user engagement as the number of DISTINCT days a user made at least one transaction during their first 7 days after signup (inclusive): signup_date through signup_date + 6 days.

Define a user as "retained" if they made at least one transaction during days 8–30 after signup (inclusive): signup_date + 7 days through signup_date + 29 days.

Assume the "current date" is 2025-06-01. To ensure a full 30-day observation window, only include users with signup_date <= 2025-05-02.

Task:

  1. Compute each included user's early distinct transaction days (0, 1, 2, ...).
  2. Bucket users into:
    • '0' for 0 early active days
    • '1' for 1 early active day
    • '2+' for 2 or more early active days
  3. For each bucket, output:
    • early_active_days_7d_bucket
    • users (number of users in the bucket)
    • retained_users (number of retained users)
    • retention_rate (retained_users / users as a decimal)

Return one row per bucket.

Table users

columntypeconstraints
user_idINTPRIMARY KEY, NOT NULL
signup_dateDATENOT NULL
countryVARCHAR(2)NOT NULL

Sample data:

countrysignup_dateuser_id
US2025-04-011
US2025-04-052
CA2025-04-103
US2025-04-154
GB2025-05-015
US2025-05-156

Table transactions

columntypeconstraints
txn_idINTPRIMARY KEY, NOT NULL
user_idINTNOT NULL
txn_tsTIMESTAMPNOT NULL
amountDECIMAL(10,2)NOT NULL

Sample data:

amounttxn_idtxn_tsuser_id
201012025-04-01 10:00:001
151022025-04-03 12:00:001
301032025-04-09 09:00:001
52012025-04-05 08:00:002
72022025-04-06 11:00:002
102032025-04-07 16:30:002
123012025-04-20 14:15:003
504012025-04-16 09:00:004
254022025-04-16 18:45:004
404032025-05-10 10:00:004
85012025-05-02 13:20:005
95022025-05-07 19:05:005
115032025-05-08 09:30:005
1006012025-05-15 08:00:006
606022025-05-25 12:00:006

Expected output

One row per early-activity bucket showing number of users, number retained, and retention rate among users with signup_date <= 2025-05-02.

early_active_days_7d_bucketretained_usersretention_rateusers
0111
1111
2+20.66666666673

Hints

  1. Use conditional aggregation to count distinct transaction dates in the first 7 days after signup.
  2. Compute retention as an existence check (MAX of a 0/1 flag) over the day 8–30 window.
  3. Filter to users with signup_date <= '2025-05-02' so every included user can have a full 30-day window before 2025-06-01.