Design a Reporting System to Generate Top 10 Movies by Category and Time Frame

Difficulty: EASYCategory: LLDFrequency: 1Asked On 2024-05-12

Problem Statement

Build a backend system that can generate a daily report of the Top 10 most-watched movies, per category (genre) and time frame (daily/weekly/monthly), based on user viewership logs.

🎯 Functional Requirements

1.Input is the users_viewership table:
2.ColumnTypeDescription
3.userid
4.STRING
5.Unique ID of user
6.movieid
7.STRING
8.Movie ID being watched
9.date
10.DATE
11.Date of the viewing session
12.starttime
13.DATETIME
14.Start timestamp of viewing session
15.endtime
16.DATETIME
17.End timestamp of viewing session
18.
19.A view is counted only if the user has watched ≥80% of the movie runtime.
20.The report should return Top 10 movies per:
•category (e.g., Action, Comedy)
•time frame (e.g., daily, weekly, monthly)
21.Report should be updated daily.


🧠 Step 1: Algorithm to Calculate Valid Views (Merge Intervals)


🔧 Assumptions

•Users may watch movies in multiple sessions (e.g., pause and resume).
•We must merge overlapping or contiguous intervals for each (userId, movieId, date).


🔁 Steps:

1.Group by userId, movieId, date
2.Merge sessions (using Merge Intervals logic)
3.Calculate total watch time per movie per user
4.If total watch time ≥ 80% of runtime → count as a valid view


🗂 Step 2: Data Model Design


🧩 Source Tables

1. users_viewership (already given)

2. movies

ColumnTypeDescription

movieid

STRING

Primary key

title

STRING

Movie name

category

STRING

Genre (Action, etc.)

runtime

INT

Total duration (mins)


3. users

ColumnTypeDescription

userid

STRING

Primary key

name

STRING

User name

region

STRING

Optional


🧱 Data Warehouse Tables

🔸 Dimension Tables

•dim_movies — from movies
•dim_users — from users
•dim_date — normalized calendar dimension (day, week, month)

🔹 Fact Table

•fact_views:
•userid
•movieid
•date_id (FK to dim_date)
•watch_time (merged total time)
•is_valid_view (boolean)
•view_count (1 if valid view else 0)

⚙️ Step 3: ETL Strategy

Daily ETL Pipeline

1.Extract users_viewership logs from the previous day.
2.Merge viewing sessions per (userId, movieId, date) using Merge Intervals logic.
3.Join with movies to get runtime and compute is_valid_view.
4.Load into fact_views.
5.Aggregate valid view counts per movie, category, and date/week/month.
6.Insert results into report_top_movies.

🧾 Step 4: SQL Queries

1. Merge Intervals and Compute Watch Time



1sql 2CopyEdit 3-- Assuming sessions are ordered 4WITH ordered_sessions AS ( 5 SELECT 6 userid, movieid, date, starttime, endtime, 7 ROW_NUMBER() OVER (PARTITION BY userid, movieid, date ORDER BY starttime) as rn 8 FROM users_viewership 9), 10merged_intervals AS ( 11 SELECT 12 userid, movieid, date, 13 MIN(starttime) AS session_start, 14 MAX(endtime) AS session_end, 15 SUM(EXTRACT(EPOCH FROM endtime - starttime)/60) AS total_watch_time 16 FROM ordered_sessions 17 GROUP BY userid, movieid, date 18), 19valid_views AS ( 20 SELECT 21 mi.userid, 22 mi.movieid, 23 mi.date, 24 mi.total_watch_time, 25 CASE WHEN mi.total_watch_time >= 0.8 * m.runtime THEN 1 ELSE 0 END AS is_valid_view 26 FROM merged_intervals mi 27 JOIN movies m ON mi.movieid = m.movieid 28)

2. Load into Fact Table (fact_views)

1sql 2CopyEdit 3INSERT INTO fact_views (userid, movieid, date_id, watch_time, is_valid_view, view_count) 4SELECT 5 vv.userid, 6 vv.movieid, 7 d.date_id, 8 vv.total_watch_time, 9 vv.is_valid_view, 10 vv.is_valid_view -- 1 or 0 11FROM valid_views vv 12JOIN dim_date d ON d.date = vv.date;

3. Generate Top 10 Movies Report (Daily)

1sql 2CopyEdit 3CREATE TABLE report_top_movies AS 4SELECT 5 m.category, 6 fv.date_id, 7 mv.movieid, 8 mv.title, 9 SUM(fv.view_count) AS total_valid_views, 10 RANK() OVER (PARTITION BY m.category, fv.date_id ORDER BY SUM(fv.view_count) DESC) as rnk 11FROM fact_views fv 12JOIN dim_movies mv ON fv.movieid = mv.movieid 13JOIN dim_date d ON fv.date_id = d.date_id 14JOIN movies m ON m.movieid = mv.movieid 15WHERE fv.is_valid_view = 1 16GROUP BY m.category, fv.date_id, mv.movieid, mv.title 17HAVING RANK() <= 10;

✅ Final Output Table: report_top_movies

categorydate_idmovieidtitletotal_valid_viewsrnk

Action

20250711

m123

Fast X

1020

1

Comedy

20250711

m555

Hangover

900

2


✅ Evaluation Criteria

•Correctness: Accurate view counting with interval merging.
•Data Modeling: Clean separation of facts and dimensions.
•Scalability: Easily expandable to weekly/monthly reports.
•ETL Design: Idempotent and daily scheduled.
•SQL Clarity: Modular and query-optimized.


Solution

Comments