cswork / libraryOpen practice workspace

Coding questions · Meta

Calculate Daily Harmful Content View Percentage

This question evaluates proficiency in data manipulation and aggregation for computing event-based metrics, testing competency in SQL/Python data processing within the Data Manipulation (SQL/Python) domain.

VIDEOS

+----+-------------+------------+
| id | uploader_id | is_harmful |
+----+-------------+------------+
| 1  | 101         | true       |
| 2  | 102         | false      |
| 3  | 101         | true       |
+----+-------------+------------+

VIEWS

+----------+---------+---------------------+
| video_id | user_id | view_time           |
+----------+---------+---------------------+
| 1        | 1001    | 2023-07-01 10:00:00 |
| 2        | 1002    | 2023-07-01 10:01:00 |
| 1        | 1003    | 2023-07-01 10:02:00 |
+----------+---------+---------------------+
Scenario

A video-sharing platform wants to quantify how often users watch harmful content.

Question

Using the provided tables, write a SQL query that returns daily view_prevalence – the percentage of all views in a day that were on videos flagged as harmful.

Hints

Join videos to views; use SUM(CASE…) / COUNT(*) grouped by DATE(view_time).

Daily Harmful Content View Prevalence

Using the VIDEOS and VIEWS tables, write a SQL query that returns daily view_prevalence – the percentage of all views in a given calendar day that were on videos flagged as harmful (is_harmful = true). Return one row per day with the date and the harmful view percentage, rounded to two decimal places.

Table VIDEOS

columntypeconstraints
idINTEGERPRIMARY KEY, NOT NULL
uploader_idINTEGERNOT NULL
is_harmfulBOOLEANNOT NULL

Sample data:

idis_harmfuluploader_id
1true101
2false102
3true101

Table VIEWS

columntypeconstraints
video_idINTEGERNOT NULL, FOREIGN KEY REFERENCES VIDEOS(id)
user_idINTEGERNOT NULL
view_timeTIMESTAMPNOT NULL

Sample data:

user_idvideo_idview_time
100112023-07-01 10:00:00
100222023-07-01 10:01:00
100312023-07-01 10:02:00

Expected output

For each calendar date, return the date and the percentage of views that were on harmful videos (is_harmful = true). Percentage is rounded to two decimals.

view_dateview_prevalence
2023-07-0166.67

Hints

  1. Join VIDEOS to VIEWS on video_id = id.
  2. Compute SUM(CASE WHEN is_harmful THEN 1 ELSE 0 END) / COUNT(*) per day.
  3. Group by DATE(view_time).