KatInterview

Calculate Engaged User Percentage by Region

SQL & DataAuto-graded · 14 test cases
MediumAuto-gradedSQL & Datadata-manipulationaggregationfiltering

Last updated

Classify users as engaged based on activity frequency and device diversity, then compute the engaged-user rate per region.

You have a list of playback events from a streaming service. Each event is a dictionary with these fields:

  • user_id: identifier of the user
  • track_id: identifier of the track played
  • device: the device type used ("app", "web", or "car")
  • event_ts: timestamp of the event in ISO format
  • region_code: the region where the user was located

You need to analyze user engagement within a 7-day window that includes a given today date plus the 6 preceding days (7 calendar days total, determined by the date portion of event_ts).

Within each region, a user is considered active if they have at least one playback event during the window. A user is considered engaged if they meet both of these criteria during the window:

  1. They played content on at least 4 distinct calendar days
  2. On at least one of those days, they used 3 or more distinct device types

Note that the device diversity requirement must be satisfied within a single day—using three different devices across three separate days does not count.

A user may appear in multiple regions if they were active in different regions during the window. Each user-region combination should be evaluated independently.

Return a list of dictionaries, one per region that has at least one active user, containing:

  • region_code: the region identifier
  • active_users: count of active users in that region
  • power_users: count of engaged users in that region
  • power_user_pct: percentage of active users who are engaged (power_users / active_users * 100), rounded to 2 decimal places

Sort the results by power_user_pct descending, then by region_code ascending.

Constraints

  • 1 ≤ number of events ≤ 100,000
  • today is a valid ISO date string (YYYY-MM-DD)
  • All event_ts values are valid ISO datetime strings
  • Each event has all required fields
  • Device types are limited to "app", "web", and "car"
  • Region codes are non-empty strings

Examples

Example 1

Input: calculate_engaged_user_rate([{'user_id': 'u1', 'track_id': 'a', 'device': 'app', 'event_ts': '2026-10-16T10:00:00', 'region_code': 'US'}, {'user_id': 'u1', 'track_id': 'b', 'device': 'app', 'event_ts': '2026-10-17T10:00:00', 'region_code': 'US'}, {'user_id': 'u1', 'track_id': 'c', 'device': 'web', 'event_ts': '2026-10-17T11:00:00', 'region_code': 'US'}, {'user_id': 'u1', 'track_id': 'd', 'device': 'app', 'event_ts': '2026-10-18T10:00:00', 'region_code': 'US'}, {'user_id': 'u1', 'track_id': 'e', 'device': 'web', 'event_ts': '2026-10-18T11:00:00', 'region_code': 'US'}, {'user_id': 'u1', 'track_id': 'f', 'device': 'car', 'event_ts': '2026-10-18T12:00:00', 'region_code': 'US'}, {'user_id': 'u1', 'track_id': 'g', 'device': 'app', 'event_ts': '2026-10-19T10:00:00', 'region_code': 'US'}, {'user_id': 'u2', 'track_id': 'h', 'device': 'app', 'event_ts': '2026-10-20T10:00:00', 'region_code': 'US'}, {'user_id': 'u3', 'track_id': 'i', 'device': 'app', 'event_ts': '2026-10-15T10:00:00', 'region_code': 'CA'}, {'user_id': 'u3', 'track_id': 'j', 'device': 'app', 'event_ts': '2026-10-16T10:00:00', 'region_code': 'CA'}, {'user_id': 'u3', 'track_id': 'k', 'device': 'app', 'event_ts': '2026-10-17T10:00:00', 'region_code': 'CA'}, {'user_id': 'u3', 'track_id': 'l', 'device': 'app', 'event_ts': '2026-10-18T10:00:00', 'region_code': 'CA'}, {'user_id': 'u3', 'track_id': 'm', 'device': 'web', 'event_ts': '2026-10-18T11:00:00', 'region_code': 'CA'}, {'user_id': 'u3', 'track_id': 'n', 'device': 'car', 'event_ts': '2026-10-18T12:00:00', 'region_code': 'CA'}, {'user_id': 'u4', 'track_id': 'o', 'device': 'app', 'event_ts': '2026-10-20T10:00:00', 'region_code': 'CA'}], '2026-10-20')

Output: [{'region_code': 'CA', 'active_users': 2, 'power_users': 1, 'power_user_pct': 50}, {'region_code': 'US', 'active_users': 2, 'power_users': 1, 'power_user_pct': 50}]

Hints

Hint 1

Group events first by user, region, and date to count distinct devices used each day

Hint 2

Track both the number of distinct active days per user and the maximum device count achieved on any single day

Hint 3

A user qualifies as engaged only when both thresholds are met: 4+ days AND 3+ devices on at least one day