KatInterview

Analyze Live Streaming Telemetry and Viewing Events

SQL & DataAuto-graded · 16 test cases
MediumAuto-gradedSQL & Datadata-aggregationtime-seriesjoins

Last updated

Process minute-level streaming activity and viewer events to calculate time-series metrics, category shares, month-over-month growth, and joined yearly summaries.

You are tasked with analyzing telemetry logs from a live streaming platform. You are given two lists of dictionaries representing minute-by-minute streaming sessions and individual viewing logs:

  • minute_streamed: A list of records where each entry represents one minute a creator was actively streaming:

    • time_minute (str): ISO timestamp string (e.g., '2019-01-01T00:00:00' or '2019-01-01 00:00:00').
    • streamer_username (str): Unique name of the streamer.
    • category (str): Content category for that minute.
    • concurrent_viewers (int): Number of active viewers at that minute.
  • minute_viewed: A list of records where each entry represents one minute spent by a viewer watching a streamer:

    • time_minute (str): Timestamp of the viewing event.
    • viewer_username (str): Unique name of the viewer.
    • viewer_country (str): Country code of the viewer (e.g., 'US').
    • streamer_username (str): Streamer being watched.

Write a Python function analyze_streaming_data that takes minute_streamed, minute_viewed, and a substring cat_keyword (str), and returns a dictionary with four specific summary datasets:

1. monthly_hours

  • Aggregate total streaming activity across all creators per calendar month formatted as 'YYYY-MM'.
  • Calculate total hours streamed (minutes / 60.0).
  • Return a list of lists: [[month_str, total_hours], ...], sorted chronologically by month_str.

2. streamer_category_totals

  • For every streamer present in minute_streamed, count:
    • total_minutes: Total minutes streamed.
    • keyword_minutes: Minutes where category contains cat_keyword (case-insensitive substring match).
    • keyword_share: Ratio keyword_minutes / total_minutes (0 if total is 0).
  • Return a list of lists: [[streamer_username, total_minutes, keyword_minutes, keyword_share], ...], sorted alphabetically by streamer_username.

3. monthly_increases

  • For each streamer and calendar month where they streamed, determine if their active minutes in that month strictly exceeded their active minutes in the immediately preceding calendar month (e.g., '2019-01' compares against '2018-12').
  • If the streamer had no streamed minutes in the preceding calendar month, count that preceding month's minutes as 0.
  • Return a list of lists: [[streamer_username, month_str, current_minutes, prev_minutes], ...], containing only months where current_minutes > prev_minutes.
  • Order the output by streamer_username ascending, then month_str ascending.

4. year_2019

  • Consider only streamers who logged at least one streamed minute during the year 2019.
  • Compute:
    • avg_concurrent: Average of concurrent_viewers across all minutes streamed by that creator in 2019 (using minute_streamed only).
    • us_viewer_minutes: Total count of viewer-minute logs in minute_viewed from viewer_country == 'US' during 2019 for that streamer, matching only timestamp minutes where the streamer was active in minute_streamed.
    • us_unique_minutes: The number of distinct streaming timestamps in 2019 where the streamer was active and had at least one US viewer log in minute_viewed.
  • Return a list of lists: [[streamer_username, avg_concurrent, us_viewer_minutes, us_unique_minutes], ...], sorted alphabetically by streamer_username.

Constraints

  • Timestamps are formatted as ISO string timestamps (space or 'T' separator).
  • Matching of cat_keyword is case-insensitive. Country code matching is exact ('US').
  • Floating point values are standard 64-bit floats.

Examples

Example 1

Input: analyze_streaming_data([{'time_minute': '2018-12-31T23:59:00', 'streamer_username': 'aaa', 'category': 'TTBHGD', 'concurrent_viewers': 100}, {'time_minute': '2019-01-01T00:00:00', 'streamer_username': 'aaa', 'category': 'TTBHGD', 'concurrent_viewers': 120}, {'time_minute': '2019-01-01T00:01:00', 'streamer_username': 'aaa', 'category': 'TTBHGD', 'concurrent_viewers': 130}, {'time_minute': '2019-03-15T09:00:00', 'streamer_username': 'bbb', 'category': 'VGDH', 'concurrent_viewers': 50}, {'time_minute': '2019-03-15T09:01:00', 'streamer_username': 'bbb', 'category': 'VGDH', 'concurrent_viewers': 60}, {'time_minute': '2019-03-31T22:15:00', 'streamer_username': 'bbb', 'category': 'CCVF', 'concurrent_viewers': 70}, {'time_minute': '2020-03-19T13:00:00', 'streamer_username': 'aaa', 'category': 'TTBHGD', 'concurrent_viewers': 133}, {'time_minute': '2020-03-19T13:01:00', 'streamer_username': 'aaa', 'category': 'TTBHGD', 'concurrent_viewers': 45}], [{'time_minute': '2019-01-01T00:00:00', 'viewer_username': 'ccc', 'viewer_country': 'US', 'streamer_username': 'aaa'}, {'time_minute': '2019-01-01T00:01:00', 'viewer_username': 'ccc', 'viewer_country': 'US', 'streamer_username': 'aaa'}, {'time_minute': '2019-03-15T09:00:00', 'viewer_username': 'ddd', 'viewer_country': 'JP', 'streamer_username': 'bbb'}, {'time_minute': '2019-03-15T09:00:00', 'viewer_username': 'eee', 'viewer_country': 'US', 'streamer_username': 'bbb'}, {'time_minute': '2019-03-15T09:01:00', 'viewer_username': 'eee', 'viewer_country': 'US', 'streamer_username': 'bbb'}, {'time_minute': '2019-03-31T22:15:00', 'viewer_username': 'fff', 'viewer_country': 'US', 'streamer_username': 'bbb'}, {'time_minute': '2020-03-19T13:00:00', 'viewer_username': 'ccc', 'viewer_country': 'US', 'streamer_username': 'aaa'}], 'TTB')

Output: {'monthly_hours': [['2018-12', 0.016666666666666666], ['2019-01', 0.03333333333333333], ['2019-03', 0.05], ['2020-03', 0.03333333333333333]], 'streamer_category_totals': [['aaa', 5, 5, 1], ['bbb', 3, 0, 0]], 'monthly_increases': [['aaa', '2018-12', 1, 0], ['aaa', '2019-01', 2, 1], ['aaa', '2020-03', 2, 0], ['bbb', '2019-03', 3, 0]], 'year_2019': [['aaa', 125, 2, 2], ['bbb', 60, 3, 3]]}

Hints

Hint 1

For month-over-month comparisons, remember that the previous calendar month for 'YYYY-01' is '(YYYY-1)-12'.

Hint 2

When filtering viewer minutes in Task 4, verify that the streamer was actually live at that exact timestamp minute.

Hint 3

Ensure keyword substring matching converts both the category and keyword to lowercase.