cswork / libraryOpen practice workspace

Coding questions · Meta

Compute percent of active users with 50+ calls

This question evaluates a data scientist's competence in Data Manipulation (SQL/Python), focusing on SQL aggregation, joining call and user snapshot tables, temporal filtering, and computing user-level engagement metrics across incoming and outgoing call records.

Problem

You work on a Messenger-like app. You want to measure how many active users in Great Britain (GB) today have been heavy callers recently.

Tables

Assume two tables (names can vary).

1) calls

One row per call record.

  • call_id (STRING, primary key)
  • caller_id (STRING)
  • receiver_id (STRING)
  • date (STRING) — call date in string format (e.g., '2025-11-06')
  • duration (INT) — call duration (seconds)

2) user_daily_status

One row per user per day describing user attributes and activity.

  • user_id (STRING)
  • country (STRING)
  • is_active (INT) — 1 if the user is active on that date, else 0
  • date (STRING) — snapshot date in string format (e.g., '2025-11-06')

Definitions / assumptions

  • “GB active users today” = users with country = 'GB', is_active = 1 on today’s snapshot date.
  • “Previous seven days” = the 7-day lookback window excluding today, i.e. [today - 7 days, today - 1 day].
  • A user’s “call records” count includes both:
    • outgoing calls where the user is the caller_id, and
    • incoming calls where the user is the receiver_id.
  • Count calls as number of call records (e.g., count of call_id).

Task

Write a SQL query to compute:

  • the percentage of today’s GB active users who had more than 50 call records in the previous 7 days.

Output

Return at least:

  • as_of_date
  • active_users_today (denominator)
  • active_users_gt_50_calls_last_7d (numerator)
  • pct_active_users_gt_50_calls_last_7d

(You may assume your SQL dialect provides a way to parse the date string into a date type.)

Percent of today's active GB users with >50 calls in the prior 7 days

You are given call logs and a daily user status table for a Messenger-like app.

Define "today" as 2025-06-01.

Compute the percentage of users who are:

  1. Active in GB (country = 'gb' and is_active = 1) on 2025-06-01, and
  2. Have more than 50 call records in the previous 7 days window FROM 2025-05-25 TO 2025-05-31 (inclusive).

A call record counts for a user if the user appears as either the caller OR the receiver. Both outgoing and incoming calls should be counted.

Notes:

  • Both tables store the date as a STRING in format 'YYYY-MM-DD'.

Return a single row with a single column: percent_active_users_with_50_plus_calls_last_7_days (as a percentage from 0 to 100, rounded to 2 decimals).

Table call_logs

columntypeconstraints
call_idBIGINTPRIMARY KEY, NOT NULL
caller_idVARCHAR(20)NOT NULL
receiver_idVARCHAR(20)NOT NULL
call_dateVARCHAR(10)NOT NULL
duration_secondsINTNOT NULL

Sample data:

call_datecall_idcaller_idduration_secondsreceiver_id
2025-05-251u160u5
2025-05-252u6120u1
2025-05-253u145u7
2025-05-254u830u1
2025-05-255u1200u9
2025-05-256u590u1
2025-05-257u175u6
2025-05-258u7110u1
2025-05-269u155u5
2025-05-2610u680u1
2025-05-2611u140u7
2025-05-2612u835u1
2025-05-2613u1140u9
2025-05-2614u565u1
2025-05-2615u195u6
2025-05-2616u7105u1
2025-05-2717u170u5
2025-05-2718u690u1
2025-05-2719u150u7
2025-05-2720u860u1
2025-05-2721u1210u9
2025-05-2722u585u1
2025-05-2723u1100u6
2025-05-2724u7115u1
2025-05-2825u165u5
2025-05-2826u675u1
2025-05-2827u155u7
2025-05-2828u825u1
2025-05-2829u1160u9
2025-05-2830u595u1
2025-05-2831u185u6
2025-05-2832u7125u1
2025-05-2933u162u5
2025-05-2934u682u1
2025-05-2935u142u7
2025-05-2936u838u1
2025-05-2937u1180u9
2025-05-2938u588u1
2025-05-2939u192u6
2025-05-2940u7112u1
2025-05-3041u158u5
2025-05-3042u678u1
2025-05-3043u148u7
2025-05-3044u828u1
2025-05-3045u1155u9
2025-05-3046u598u1
2025-05-3047u183u6
2025-05-3048u7118u1
2025-05-3149u166u5
2025-05-3150u686u1
2025-05-3151u152u7
2025-06-0152u160u5
2025-05-2453u560u1

Table user_daily_status

columntypeconstraints
user_idVARCHAR(20)NOT NULL
countryVARCHAR(2)NOT NULL
is_activeINTNOT NULL
status_dateVARCHAR(10)NOT NULL

Sample data:

countryis_activestatus_dateuser_id
gb12025-06-01u1
gb12025-06-01u2
gb02025-06-01u3
us12025-06-01u4
gb12025-05-31u1

Expected output

A single-row result showing the percent of today's (2025-06-01) active GB users who made/received >50 calls between 2025-05-25 and 2025-05-31 inclusive.

percent_active_users_with_50_plus_calls_last_7_days
50

Hints

  1. Filter the denominator to users who are active in GB on 2025-06-01.
  2. Count calls where the user is either caller or receiver in the date range 2025-05-25 through 2025-05-31.
  3. Use a LEFT JOIN so active users with zero calls are still included in the denominator.