Write SQL for content view metrics by device
Problem Description
You are a reporting analyst for a news subscription product, tasked with analyzing content view events.
Data Schema
You have access to a page_views fact table in Google BigQuery (Standard SQL), where each row represents a single page/content view event:
event_idSTRING (unique)agent_idSTRING (an identifier for the reader/user agent)device_typeSTRING (one of: 'phone', 'tab', 'desktop')content_idSTRING (nullable; may be NULL if not resolved)urlSTRINGcontent_typeSTRING (e.g., 'article', 'video', 'interactive', etc.; may be NULL)event_tsTIMESTAMP (UTC)
Definitions
- Mobile:
device_typeIN ('phone', 'tab') - Desktop:
device_type= 'desktop' - Unique content view: Counting DISTINCT
content_idvalues. NULLs should not be counted as a distinct content.
Tasks
Write SQL queries to answer each of the following:
-
Mobile vs Desktop unique contents viewed:
- Return two rows (mobile, desktop) with the number of unique contents viewed.
- Also include total view events for each device group to show the difference between "# of unique contents viewed" and "# of views".
- Output columns:
device_group,unique_contents_viewed,total_views.
-
Peak usage hours by device group:
- For each device group (mobile, desktop), find the top 3 hours of day (0–23) with the most view events.
- Use
EXTRACT(HOUR FROM event_ts). - Output columns:
device_group,hour_of_day,views,hour_rank.
-
Content ID coverage:
- Compute (a) how many unique non-NULL contents were viewed and (b) how many view events have
content_idIS NULL. - Output columns:
unique_nonnull_contents_viewed,null_content_view_events.
- Compute (a) how many unique non-NULL contents were viewed and (b) how many view events have
-
Distribution of first content type per agent per day:
- For each
agent_idand calendar date (UTC), find that agent’s first view event of the day (earliestevent_ts). - Take the
content_typeof that first event and compute the overall distribution across all agent-days. - Output columns:
content_type,agent_days,share_of_agent_days.
- For each
Notes
- If there are ties for the first event timestamp within an agent-day, break ties deterministically using
event_idascending. - You may assume a large dataset; write SQL that is idiomatic for BigQuery.
AI interview prep
Core dilemma
Balancing the correctness and precision of complex aggregations (especially with NULLs and distinct counts) with the performance and efficiency of SQL queries on a large, potentially petabyte-scale dataset in BigQuery.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Write SQL for content view metrics by device
Problem Description
You are a reporting analyst for a news subscription product, tasked with analyzing content view events.
Data Schema
You have access to a page_views fact table in Google BigQuery (Standard SQL), where each row represents a single page/content view event:
event_idSTRING (unique)agent_idSTRING (an identifier for the reader/user agent)device_typeSTRING (one of: 'phone', 'tab', 'desktop')content_idSTRING (nullable; may be NULL if not resolved)urlSTRINGcontent_typeSTRING (e.g., 'article', 'video', 'interactive', etc.; may be NULL)event_tsTIMESTAMP (UTC)
Definitions
- Mobile:
device_typeIN ('phone', 'tab') - Desktop:
device_type= 'desktop' - Unique content view: Counting DISTINCT
content_idvalues. NULLs should not be counted as a distinct content.
Tasks
Write SQL queries to answer each of the following:
-
Mobile vs Desktop unique contents viewed:
- Return two rows (mobile, desktop) with the number of unique contents viewed.
- Also include total view events for each device group to show the difference between "# of unique contents viewed" and "# of views".
- Output columns:
device_group,unique_contents_viewed,total_views.
-
Peak usage hours by device group:
- For each device group (mobile, desktop), find the top 3 hours of day (0–23) with the most view events.
- Use
EXTRACT(HOUR FROM event_ts). - Output columns:
device_group,hour_of_day,views,hour_rank.
-
Content ID coverage:
- Compute (a) how many unique non-NULL contents were viewed and (b) how many view events have
content_idIS NULL. - Output columns:
unique_nonnull_contents_viewed,null_content_view_events.
- Compute (a) how many unique non-NULL contents were viewed and (b) how many view events have
-
Distribution of first content type per agent per day:
- For each
agent_idand calendar date (UTC), find that agent’s first view event of the day (earliestevent_ts). - Take the
content_typeof that first event and compute the overall distribution across all agent-days. - Output columns:
content_type,agent_days,share_of_agent_days.
- For each
Notes
- If there are ties for the first event timestamp within an agent-day, break ties deterministically using
event_idascending. - You may assume a large dataset; write SQL that is idiomatic for BigQuery.