cswork / libraryOpen practice workspace

Coding questions · Meta

Compute High-Call Usage Rates

This question evaluates a candidate's ability to perform advanced data manipulation and analytical SQL tasks, including joins, aggregations, timezone-aware date handling, rolling window metrics, distinct user counting, and counting user participations across roles.

You are given two tables for a voice-calling product:

users

  • user_id BIGINT
  • country_code STRING

calls

  • call_id BIGINT
  • caller_id BIGINT
  • recipient_id BIGINT
  • started_at TIMESTAMP

calls.caller_id and calls.recipient_id both reference users.user_id.

Assume:

  • Every row in calls is one valid call event.
  • All timestamps are stored in UTC.
  • Any date-based metric should be computed in the Europe/London timezone.
  • report_date is a calendar date in Europe/London.

Write SQL to compute the following metrics for each report_date:

  1. Percentage of callers with at least 20 calls in the last 7 days

    • Use the 7-day window ending on report_date, inclusive.
    • The numerator is the number of distinct users who initiated at least 20 calls in that 7-day window.
    • The denominator is the number of distinct users who initiated at least 1 call in that same 7-day window.
  2. Percentage of Great Britain DAU with at least 50 total calls

    • Restrict to users with country_code = 'GB'.
    • Define daily active users as distinct GB users who either placed or received at least one call on report_date.
    • For each such user, count total call participations on report_date, where participations include both outgoing calls as caller_id and incoming calls as recipient_id.
    • The numerator is the number of GB DAU with at least 50 total call participations on report_date.
    • The denominator is all GB DAU on report_date.

Return one row per report_date with these columns:

  • report_date
  • pct_callers_20plus_last_7d
  • pct_gb_dau_50plus_calls

Compute High-Call Usage Rates

Table users

columntypeconstraints
user_idBIGINTPRIMARY KEY, NOT NULL
country_codeVARCHAR(2)NOT NULL

Sample data:

country_codeuser_id
GB1
GB2
US3
GB4
FR5

Table calls

columntypeconstraints
call_idBIGINTPRIMARY KEY, NOT NULL
caller_idBIGINTNOT NULL, FOREIGN KEY users(user_id)
recipient_idBIGINTNOT NULL, FOREIGN KEY users(user_id)
started_atTIMESTAMPNOT NULL

Sample data:

call_idcaller_idrecipient_idstarted_at
1001132023-01-07 10:00:00
1002132023-01-07 10:01:00
1003132023-01-07 10:02:00
1004132023-01-07 10:03:00
1005132023-01-07 10:04:00
1006132023-01-07 10:05:00
1007132023-01-07 10:06:00
1008132023-01-07 10:07:00
1009132023-01-07 10:08:00
1010132023-01-07 10:09:00
1011132023-01-07 10:10:00
1012132023-01-07 10:11:00
1013132023-01-07 10:12:00
1014132023-01-07 10:13:00
1015132023-01-07 10:14:00
1016132023-01-07 10:15:00
1017132023-01-07 10:16:00
1018132023-01-07 10:17:00
1019132023-01-07 10:18:00
1020132023-01-07 10:19:00
1021132023-01-07 10:20:00
1022132023-01-07 10:21:00
1023132023-01-07 10:22:00
1024132023-01-07 10:23:00
1025132023-01-07 10:24:00
1026132023-01-07 10:25:00
1027132023-01-07 10:26:00
1028132023-01-07 10:27:00
1029132023-01-07 10:28:00
1030132023-01-07 10:29:00
1031132023-01-07 10:30:00
1032132023-01-07 10:31:00
1033132023-01-07 10:32:00
1034132023-01-07 10:33:00
1035132023-01-07 10:34:00
1036132023-01-07 10:35:00
1037132023-01-07 10:36:00
1038132023-01-07 10:37:00
1039132023-01-07 10:38:00
1040132023-01-07 10:39:00
1041132023-01-07 10:40:00
1042132023-01-07 10:41:00
1043132023-01-07 10:42:00
1044132023-01-07 10:43:00
1045132023-01-07 10:44:00
1046132023-01-07 10:45:00
1047132023-01-07 10:46:00
1048132023-01-07 10:47:00
1049132023-01-07 10:48:00
1050132023-01-07 10:49:00
2001232023-01-02 09:00:00
2002252023-01-02 09:05:00
2003232023-01-02 09:10:00
2004252023-01-02 09:15:00
2005232023-01-03 09:00:00
2006252023-01-03 09:05:00
2007232023-01-03 09:10:00
2008252023-01-03 09:15:00
2009232023-01-04 09:00:00
2010252023-01-04 09:05:00
2011232023-01-04 09:10:00
2012252023-01-04 09:15:00
2013232023-01-05 09:00:00
2014252023-01-05 09:05:00
2015232023-01-05 09:10:00
2016252023-01-05 09:15:00
2017232023-01-06 09:00:00
2018252023-01-06 09:05:00
2019232023-01-06 09:10:00
3001352023-01-03 10:30:00
4001532023-01-04 10:00:00
4002532023-01-04 10:05:00
4003532023-01-04 10:10:00
4004532023-01-04 10:15:00
4005532023-01-04 10:20:00
8001232023-01-08 08:00:00
8002232023-01-08 08:01:00
8003232023-01-08 08:02:00
8004232023-01-08 08:03:00
8005232023-01-08 08:04:00
8006232023-01-08 08:05:00
8007232023-01-08 08:06:00
8008232023-01-08 08:07:00
8009232023-01-08 08:08:00
8010232023-01-08 08:09:00
8011232023-01-08 08:10:00
8012232023-01-08 08:11:00
8013232023-01-08 08:12:00
8014232023-01-08 08:13:00
8015232023-01-08 08:14:00
8016232023-01-08 08:15:00
8017232023-01-08 08:16:00
8018232023-01-08 08:17:00
8019232023-01-08 08:18:00
8020232023-01-08 08:19:00
8021232023-01-08 08:20:00
8022232023-01-08 08:21:00
8023232023-01-08 08:22:00
8024232023-01-08 08:23:00
8025232023-01-08 08:24:00
8026232023-01-08 08:25:00
8027232023-01-08 08:26:00
8028232023-01-08 08:27:00
8029232023-01-08 08:28:00
8030232023-01-08 08:29:00
8031232023-01-08 08:30:00
8032232023-01-08 08:31:00
8033232023-01-08 08:32:00
8034232023-01-08 08:33:00
8035232023-01-08 08:34:00
8036232023-01-08 08:35:00
8037232023-01-08 08:36:00
8038232023-01-08 08:37:00
8039232023-01-08 08:38:00
8040232023-01-08 08:39:00
8041232023-01-08 08:40:00
8042232023-01-08 08:41:00
8043232023-01-08 08:42:00
8044232023-01-08 08:43:00
8045232023-01-08 08:44:00
8046232023-01-08 08:45:00
8047232023-01-08 08:46:00
8048232023-01-08 08:47:00
8049232023-01-08 08:48:00
8050232023-01-08 08:49:00

Expected output

One row per report_date with the two requested percentage metrics. Percentages are returned as decimal fractions between 0 and 1.

pct_callers_20plus_last_7dpct_gb_dau_50plus_callsreport_date
0.2512023-01-07
0.512023-01-08

Hints

  1. Derive report_date as DATE(started_at). For January dates, Europe/London is aligned with UTC, so no offset is needed.
  2. Build the 7-day inclusive window as [report_date - 6 days, report_date] and aggregate outgoing calls per caller over that window.
  3. Compute GB DAU by unioning caller and recipient user_ids per day, joining to users where country_code = 'GB', and counting participations per user.