cswork / libraryOpen practice workspace

Coding questions · Meta

Analyze Cross-Country Call Data for Recent Trends

This question evaluates a candidate's competency in relational data manipulation, including joining tables, aggregations, time-based filtering, and duration calculations using SQL or equivalent Python tools.

Users

+---------+-------------+----------+
| user_id | signup_date | country  |
+---------+-------------+----------+
| 1       | 2021-01-04  | US       |
| 2       | 2021-02-10  | CA       |
| 3       | 2021-03-17  | US       |
| 4       | 2021-04-23  | IN       |
| 5       | 2021-05-05  | GB       |
+---------+-------------+----------+

Calls

+---------+-----------+-------------+---------------------+---------------------+
| call_id | caller_id | receiver_id | start_time          | end_time            |
+---------+-----------+-------------+---------------------+---------------------+
| 1001    | 1         | 2           | 2022-07-01 10:00:00 | 2022-07-01 10:15:00 |
| 1002    | 3         | 1           | 2022-07-01 11:00:00 | 2022-07-01 11:05:00 |
| 1003    | 4         | 5           | 2022-07-02 09:30:00 | 2022-07-02 09:45:00 |
| 1004    | 2         | 4           | 2022-07-02 12:00:00 | 2022-07-02 12:25:00 |
| 1005    | 5         | 3           | 2022-07-03 08:00:00 | 2022-07-03 08:30:00 |
+---------+-----------+-------------+---------------------+---------------------+
Scenario

Video-calling product team wants insights on cross-country usage.

Question

Using the tables below, write a SQL query that returns, for each pair of caller_country and receiver_country, the total number of completed calls and average call duration in the last 30 days.

Hints

Join Calls to Users twice (caller and receiver), filter last-30-days, compute duration as TIMESTAMPDIFF/datediff, group by both countries.

Cross-country call metrics over a 30-day window

Using the Users and Calls tables, write a SQL query that returns, for each pair of caller_country and receiver_country, the total number of completed calls and the average call duration in minutes. Consider only calls whose start_time is in the 30-day window from 2022-07-01 (inclusive) to 2022-07-31 (exclusive). Treat a call as completed if end_time is not NULL and end_time > start_time.

Table Users

columntypeconstraints
user_idINTEGERPRIMARY KEY, NOT NULL
signup_dateDATENOT NULL
countryCHAR(2)NOT NULL

Sample data:

countrysignup_dateuser_id
US2021-01-041
CA2021-02-102
US2021-03-173
IN2021-04-234
GB2021-05-055

Table Calls

columntypeconstraints
call_idINTEGERPRIMARY KEY, NOT NULL
caller_idINTEGERNOT NULL, FOREIGN KEY REFERENCES Users(user_id)
receiver_idINTEGERNOT NULL, FOREIGN KEY REFERENCES Users(user_id)
start_timeDATETIMENOT NULL
end_timeDATETIMENOT NULL

Sample data:

call_idcaller_idend_timereceiver_idstart_time
100112022-07-01 10:15:0022022-07-01 10:00:00
100232022-07-01 11:05:0012022-07-01 11:00:00
100342022-07-02 09:45:0052022-07-02 09:30:00
100422022-07-02 12:25:0042022-07-02 12:00:00
100552022-07-03 08:30:0032022-07-03 08:00:00

Expected output

One row per (caller_country, receiver_country) pair with the total number of completed calls and the average call duration in minutes, limited to calls with start_time in [2022-07-01, 2022-07-31), end_time not NULL, and end_time > start_time. For the provided sample data (all calls are in-window and completed), the output is:

avg_duration_minutescaller_countryreceiver_countrytotal_calls
25CAIN1
30GBUS1
15INGB1
15USCA1
5USUS1

Hints

  1. Join Calls to Users twice: once on caller_id and once on receiver_id.
  2. Filter using the fixed window [2022-07-01, 2022-07-31).
  3. In PostgreSQL, compute duration in minutes as EXTRACT(EPOCH FROM (end_time - start_time)) / 60.0.
  4. Group by caller_country and receiver_country.