Compute Each Viewer's Share of YouTube Watch Time by Category
Last updated: November 29, 2026
Quick Overview
Given YouTube view rows with category and watch seconds, compute each user's watch time share per category, mark their top category, and flag users who spend at least 80 percent of their time in one category.
0
0
0 solved
Given YouTube view rows with category and watch seconds, compute each user's watch time share per category, mark their top category, and flag users who spend at least 80 percent of their time in one category.
A pandas warm-up framed around YouTube recommendations, where knowing how concentrated a viewer's interests are informs diversity work. The interviewer mainly checks groupby transform, which keeps row-level detail while attaching group totals without a merge.
What the Interviewer Expects
- Aggregate to user and category before computing shares
- Use transform('sum') to attach each user's total
- Flag top categories, keeping ties
- Exclude zero or negative watch time rows
- Explain transform versus agg plus merge
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you measure concentration with a single number per user, such as entropy or HHI?
- How would you weight Shorts versus long-form watch time?
- How would you check whether focused users have lower retention?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
watch(user_id, video_id, category, watch_seconds), one row per view. Rows with zero seconds (immediate skips) carry no time and are dropped. Output one row per user and category with share, an `is_t...
Approach
Sum watch seconds per (user, category). groupby('user_id').transform('sum') returns the user total aligned to each row, so share is a simple division. The same trick with transform('max') marks th...