KatInterview

Live Stream Creator Stats: Weekly, Monthly Rankings and Daily Check

SQL & DataAuto-graded · 17 test cases
MediumAuto-gradedSQL & Datajoinsgroup-byranking

Last updated

Join two record lists on a composite key with missing-row defaults, then compute a filtered distinct count, a per-group top-5 ranking with tie-breaks, and an every-item condition check.

Live Stream Creator Stats

A streaming platform stores two lists of records. You must compute three metrics from them in a single function call.

Input records

live_room is a list of dictionaries, one per live stream (a "room"):

keytypemeaning
datestringISO date (YYYY-MM-DD) on which the room was live
creator_idintegercreator who started the room
room_idintegerroom identifier
countrystringcountry the room is attributed to (for example US, ID, UK)
live_typestringdevice used to stream (for example mobile, pc)

The pair (date, room_id) is unique in this list.

live_room_activity is a list of dictionaries holding engagement totals for a room on a date:

keytypemeaning
datestringISO date, same as the room's date
room_idintegerroom identifier
watch_cntintegernumber of viewers
gift_recieved_cntintegernumber of gifts received (the key is spelled exactly this way)

The pair (date, room_id) is unique in this list too.

run_date is an ISO date string representing the day the computation is run.

Matching rooms to activity

A room is matched to its activity record using both date and room_id. If a room has no matching activity record, treat both its watch_cnt and its gift_recieved_cnt as 0. Activity records that match no room are ignored.

Metric 1: q1

Count the distinct creators who launched more than 3 rooms that satisfy all of the following:

  • date between 2022-10-01 and 2022-10-07, inclusive
  • country equals US
  • live_type equals mobile

Result: {"creator_count": <int>}.

Metric 2: q2

Consider only rooms whose date falls in October 2022. For each pair (country, creator), the total gifts is the sum of gift_recieved_cnt (with the default of 0 for unmatched rooms) over that creator's rooms in that country. A creator with rooms in several countries is treated separately in each country.

Within each country, rank creators by total gifts descending; ties are broken by creator_id ascending. Ranks are consecutive positions 1, 2, 3, ... (tied totals still receive different ranks). Keep only the first 5 creators of each country (fewer if the country has fewer creators).

Result: a list of dictionaries, each with keys country, creator_id, total_gifts_received, rank_in_country. Order the list by country ascending (string order), and within a country by rank ascending. If there are no October rooms, the list is empty.

Metric 3: q3

Let yesterday be the calendar day immediately before run_date. Count the creators who satisfy both:

  1. They launched at least 2 rooms on yesterday's date.
  2. Every room they launched on that date has watch_cnt >= 500 (a room with no activity record counts as 0 and therefore fails).

Result: {"qualified_creator_count": <int>}.

Return value

Return a dictionary:

{
  "q1": {"creator_count": ...},
  "q2": [ {...}, ... ],
  "q3": {"qualified_creator_count": ...}
}

Constraints

  • Dates are valid ISO strings, so string comparison matches chronological order.
  • Both input lists may be empty.
  • (date, room_id) is unique within each list.
  • "More than 3" means 4 or more; "at least 2" means 2 or more; the viewer threshold is inclusive (500 passes).
  • Rooms outside the date, country or device filters of a metric must not contribute to that metric.

Examples

Example 1

Input: creator_live_stats([{'date': '2022-10-01', 'creator_id': 1, 'room_id': 101, 'country': 'US', 'live_type': 'mobile'}, {'date': '2022-10-02', 'creator_id': 1, 'room_id': 102, 'country': 'US', 'live_type': 'mobile'}, {'date': '2022-10-03', 'creator_id': 1, 'room_id': 103, 'country': 'US', 'live_type': 'mobile'}, {'date': '2022-10-04', 'creator_id': 1, 'room_id': 104, 'country': 'US', 'live_type': 'mobile'}, {'date': '2022-10-05', 'creator_id': 2, 'room_id': 105, 'country': 'US', 'live_type': 'mobile'}, {'date': '2022-10-06', 'creator_id': 2, 'room_id': 106, 'country': 'US', 'live_type': 'pc'}, {'date': '2022-10-31', 'creator_id': 1, 'room_id': 107, 'country': 'US', 'live_type': 'mobile'}, {'date': '2022-11-01', 'creator_id': 1, 'room_id': 108, 'country': 'US', 'live_type': 'mobile'}, {'date': '2022-11-01', 'creator_id': 1, 'room_id': 109, 'country': 'US', 'live_type': 'mobile'}, {'date': '2022-11-01', 'creator_id': 2, 'room_id': 110, 'country': 'US', 'live_type': 'mobile'}, {'date': '2022-11-01', 'creator_id': 2, 'room_id': 111, 'country': 'US', 'live_type': 'mobile'}], [{'date': '2022-10-01', 'room_id': 101, 'watch_cnt': 600, 'gift_recieved_cnt': 10}, {'date': '2022-10-02', 'room_id': 102, 'watch_cnt': 600, 'gift_recieved_cnt': 20}, {'date': '2022-10-03', 'room_id': 103, 'watch_cnt': 600, 'gift_recieved_cnt': 30}, {'date': '2022-10-04', 'room_id': 104, 'watch_cnt': 600, 'gift_recieved_cnt': 40}, {'date': '2022-10-31', 'room_id': 107, 'watch_cnt': 700, 'gift_recieved_cnt': 100}, {'date': '2022-11-01', 'room_id': 108, 'watch_cnt': 500, 'gift_recieved_cnt': 3}, {'date': '2022-11-01', 'room_id': 109, 'watch_cnt': 800, 'gift_recieved_cnt': 4}, {'date': '2022-11-01', 'room_id': 110, 'watch_cnt': 499, 'gift_recieved_cnt': 5}, {'date': '2022-11-01', 'room_id': 111, 'watch_cnt': 500, 'gift_recieved_cnt': 6}], '2022-11-02')

Output: {'q1': {'creator_count': 1}, 'q2': [{'country': 'US', 'creator_id': 1, 'total_gifts_received': 200, 'rank_in_country': 1}, {'country': 'US', 'creator_id': 2, 'total_gifts_received': 0, 'rank_in_country': 2}], 'q3': {'qualified_creator_count': 1}}

Example 2

Input: creator_live_stats([{'date': '2022-10-01', 'creator_id': 10, 'room_id': 1, 'country': 'ID', 'live_type': 'mobile'}, {'date': '2022-10-02', 'creator_id': 11, 'room_id': 2, 'country': 'ID', 'live_type': 'pc'}, {'date': '2022-10-03', 'creator_id': 12, 'room_id': 3, 'country': 'UK', 'live_type': 'mobile'}, {'date': '2022-10-04', 'creator_id': 10, 'room_id': 4, 'country': 'ID', 'live_type': 'pc'}, {'date': '2022-10-05', 'creator_id': 11, 'room_id': 5, 'country': 'ID', 'live_type': 'mobile'}, {'date': '2022-10-06', 'creator_id': 12, 'room_id': 6, 'country': 'UK', 'live_type': 'pc'}], [{'date': '2022-10-01', 'room_id': 1, 'watch_cnt': 1, 'gift_recieved_cnt': 5}, {'date': '2022-10-02', 'room_id': 2, 'watch_cnt': 1, 'gift_recieved_cnt': 5}, {'date': '2022-10-03', 'room_id': 3, 'watch_cnt': 1, 'gift_recieved_cnt': 20}, {'date': '2022-10-04', 'room_id': 4, 'watch_cnt': 1, 'gift_recieved_cnt': 5}, {'date': '2022-10-05', 'room_id': 5, 'watch_cnt': 1, 'gift_recieved_cnt': 5}, {'date': '2022-10-06', 'room_id': 6, 'watch_cnt': 1, 'gift_recieved_cnt': 1}], '2022-10-10')

Output: {'q1': {'creator_count': 0}, 'q2': [{'country': 'ID', 'creator_id': 10, 'total_gifts_received': 10, 'rank_in_country': 1}, {'country': 'ID', 'creator_id': 11, 'total_gifts_received': 10, 'rank_in_country': 2}, {'country': 'UK', 'creator_id': 12, 'total_gifts_received': 21, 'rank_in_country': 1}], 'q3': {'qualified_creator_count': 0}}

Example 3

Input: creator_live_stats([], [], '2023-01-01')

Output: {'q1': {'creator_count': 0}, 'q2': [], 'q3': {'qualified_creator_count': 0}}

Hints

Hint 1

Build a dictionary keyed by (date, room_id) from the activity list so each room can look up its engagement in constant time, falling back to (0, 0) when missing.

Hint 2

For q2, aggregate totals per (country, creator), group these by country, then sort each group with the key (-total, creator_id) and slice the first 5 before assigning ranks 1..5.

Hint 3

For q3, compute yesterday with datetime arithmetic, group that day's rooms by creator, and require both len(rooms) >= 2 and all(watch >= 500) where missing activity means watch = 0.