Video Call Metrics: Regional Reach and Per-User Talk Time
SQL & DataModel answerLast updated
Write two SQL queries over daily call and profile tables: the share of French users on a call yesterday, and total U.S. call time divided by the number of U.S. daily-active users today.
Context
A video-calling product tracks how engaged its users are. You have two tables that are partitioned by day.
call_logs: one row per call.
| column | meaning |
|---|---|
caller | ID of the user who started the call |
recipient | ID of the user who received the call |
ds | date of the call |
call_id | identifier of the call |
duration | length of the call (float, in seconds) |
user_profile: one daily snapshot row per user.
| column | meaning |
|---|---|
user_id | user identifier |
age_bucket | age group, e.g. 25-34 |
country | country, e.g. US, FR |
primary_os | e.g. iOS, Android |
dau_flag | 1 if the user was a daily active user that day, otherwise 0 |
ds | date of the snapshot |
Sample rows:
call_logs user_profile
caller recipient ds call_id duration user_id age_bucket country primary_os dau_flag ds
123 456 2019-01-01 4325 864.4 123 25-34 US iOS 1 2019-01-01
032 789 2019-01-01 9395 263.7 456 35-44 FR Android 1 2019-01-01
456 032 2019-01-01 0879 22.0 789 18-24 FR iOS 0 2019-01-01
Tasks
- Write a query that returns the percentage of French users who made or received at least one call yesterday.
- Write a query that returns today's total call time divided by the number of U.S. daily-active users (users with
dau_flag = 1).
What to address
- State your assumptions about how the country is stored (the sample uses codes such as
FR, not full names). - Make sure no user and no call is counted twice.
- Decide what happens for users with no calls and for days with no data.
- Explain why each numerator and denominator is right.
Hints
Hint 1
Both answers are ratios, so decide the numerator and denominator separately. For the first query, the denominator is every French user in yesterday's snapshot, including those who never called.
Hint 2
A user can appear as a caller, as a recipient, or both, and many times. Stack the two columns into a single list of distinct user IDs (UNION rather than UNION ALL) and then match it against the French users. Remember that the profile table has one row per user per day, so filter it to a single date.
Hint 3
For the second query, a call between two U.S. DAU must contribute its duration only once. Reduce to one row per call that has at least one U.S. DAU participant, sum the durations, and divide by a separately computed count of U.S. DAU, so that users without calls still count in the denominator.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Both metrics are ratios, so most mistakes come from a wrong numerator or denominator. I would settle a few definitions before writing SQL.
Clarifications
- Country values. The sample stores codes (
'FR','US'). I use the codes and would switch to'France'only if the column holds full names. - "Placed" vs. "made or received". The question says made or received, so recipients count too. Counting only callers would be a different, narrower metric.
- Snapshot table.
user_profilehas one row per user perds. Without a date filter every count is inflated by the number of days stored. - ID types. IDs such as
032have leading zeros, so they are likely strings. The join keys must have the same type on both sides.
Query 1: share of French users with a call yesterday
- Denominator: all French users in yesterday's snapshot.
- Numerator: those French users who appear in yesterday's calls as caller or recipient.
WITH fr_users AS (
SELECT DISTINCT user_id
FROM user_profile
WHERE country = 'FR'
AND ds = CURRENT_DATE - INTERVAL '1' DAY -- DATE_SUB(CURRENT_DATE, INTERVAL 1 DAY) in MySQL
),
active_yesterday AS (
SELECT caller AS user_id
FROM call_logs
WHERE ds = CURRENT_DATE - INTERVAL '1' DAY
UNION -- UNION dedupes, UNION ALL would not
SELECT recipient
FROM call_logs
WHERE ds = CURRENT_DATE - INTERVAL '1' DAY
)
SELECT 100.0 * COUNT(a.user_id) / NULLIF(COUNT(f.user_id), 0) AS pct_fr_users_with_call
FROM fr_users f
LEFT JOIN active_yesterday a ON a.user_id = f.user_id;
Why it works:
fr_usershas one row per user andactive_yesterdayis deduplicated, so the left join cannot multiply rows.- Starting from the French users and left-joining keeps those who made no calls. An inner join would drop them and always return 100%.
COUNT(a.user_id)ignores NULLs, so it counts only matched users.100.0forces decimal division, andNULLIFavoids a division-by-zero error when there are no French users.
Query 2: today's call time per U.S. DAU
- Denominator: number of U.S. users with
dau_flag = 1in today's snapshot. - Numerator: total duration of today's calls with at least one such user, with each call counted once.
WITH us_dau AS (
SELECT DISTINCT user_id
FROM user_profile
WHERE country = 'US' AND dau_flag = 1 AND ds = CURRENT_DATE
),
us_calls AS (
SELECT DISTINCT cl.call_id, cl.duration
FROM call_logs cl
LEFT JOIN us_dau c ON cl.caller = c.user_id
LEFT JOIN us_dau r ON cl.recipient = r.user_id
WHERE cl.ds = CURRENT_DATE
AND (c.user_id IS NOT NULL OR r.user_id IS NOT NULL)
)
SELECT COALESCE(SUM(duration), 0)
/ NULLIF((SELECT COUNT(*) FROM us_dau), 0) AS call_time_per_us_dau
FROM us_calls;
Why it works:
- Each join hits a deduplicated set, so a row matches at most once. A call between two U.S. DAU stays a single row, and
DISTINCT call_idalso guards against duplicate log rows. - The denominator comes from its own subquery, so DAU with no calls still lower the average, which is what "per DAU" means.
COALESCEreturns 0 on a day with no calls.NULLIFreturns NULL rather than an error when there are no U.S. DAU.
Alternative definition. If the interviewer wants each user's own talk time, a call between two U.S. DAU counts for both people. Then unpivot the calls into (user_id, duration) rows with UNION ALL over caller and recipient, inner-join to us_dau, and sum. I would state which definition I chose and why.
Complexity
With C call rows and U profile rows for the day, hash joins and hash-based dedup give O(C + U) time and O(C + U) space. Sort-based plans cost O((C + U) log(C + U)). Filtering on the ds partition first is the main performance step, because both tables are partitioned by day.
Edge cases
- No French users or no U.S. DAU: NULL instead of a division error.
- No calls today: the total is 0, not NULL.
- Self-calls: handled by
UNIONandDISTINCT call_id. - NULL caller or recipient: never matches a profile row, so it is ignored.
- Partial "today" data: today's partition may still be loading, so many teams report on yesterday instead. Worth mentioning.
- Country changes over time: joining on the same-day snapshot avoids misattributing users.
Common mistakes
- Forgetting to filter
user_profilebyds, which inflates every count. - Using
UNION ALL, or joining calls directly to users, so heavy callers are counted many times. - Inner-joining in Query 1, which makes the denominator only active users.
- Summing duration after joining twice without deduplicating calls, which doubles U.S.-to-U.S. calls.
- Integer division producing 0 in engines like Postgres or SQL Server.
- Dividing by the number of U.S. users who called rather than by all U.S. DAU.
Pandas equivalent
import pandas as pd
def fr_call_pct(calls, profile, day):
day = pd.Timestamp(day)
fr = set(profile.loc[(profile.ds == day) & (profile.country == "FR"), "user_id"])
c = calls[calls.ds == day]
active = set(c.caller) | set(c.recipient)
return 100.0 * len(fr & active) / len(fr) if fr else None
def us_time_per_dau(calls, profile, day):
day = pd.Timestamp(day)
dau = set(profile.loc[(profile.ds == day) & (profile.country == "US")
& (profile.dau_flag == 1), "user_id"])
if not dau:
return None
c = calls[calls.ds == day].drop_duplicates("call_id")
m = c.caller.isin(dau) | c.recipient.isin(dau)
return c.loc[m, "duration"].sum() / len(dau)
A strong answer states the definitions first, gets both denominators right, and explains how each query avoids double counting.