KatInterview

Rolling Window Metrics and Multi-Role Activity Analysis

SQL & DataAuto-graded
HardAuto-gradedSQL & Datasqlaggregationwindow-functions

Last updated

Build SQL queries to calculate rolling 7-day caller engagement rates and single-day participation thresholds for geo-filtered active users.

You have two tables representing activity in a voice communication platform:

users

  • user_id BIGINT
  • country_code STRING

calls

  • call_id BIGINT
  • caller_id BIGINT (references users.user_id)
  • recipient_id BIGINT (references users.user_id)
  • started_at TIMESTAMP

All timestamps in the calls table are recorded in UTC. For reporting purposes, all date-based calculations must use the Europe/London timezone.

Write a SQL query that produces daily metrics with the following columns:

  • report_date: a calendar date in the London timezone
  • pct_callers_20plus_last_7d: percentage of users who placed 20 or more calls during the 7-day period ending on (and including) report_date
    • Numerator: count of distinct users who initiated ≥ 20 calls in the 7-day window
    • Denominator: count of distinct users who initiated ≥ 1 call in the same 7-day window
  • pct_gb_dau_50plus_calls: percentage of Great Britain daily active users who participated in 50 or more calls on report_date
    • Restrict to users where country_code = 'GB'
    • A user is active on a given day if they either placed or received at least one call that day
    • Count total participations for each active user, where a participation is either being the caller or being the recipient
    • Numerator: count of active GB users with ≥ 50 participations on report_date
    • Denominator: count of all active GB users on report_date

Return one row per report_date, ordered chronologically.

Hints

Hint 1

Convert timestamps to the London timezone before extracting the date—DST transitions mean a UTC midnight does not align with a London day boundary

Hint 2

For rolling windows over distinct users, generate a calendar of dates and join to pre-aggregated daily counts rather than using row-based window functions

Hint 3

Counting participations across both caller and recipient roles requires combining two views of the same call event