Completion Rate by Video Duration Bucket in Pandas

Last updated: November 29, 2026

Quick Overview

Given a views DataFrame (video_id, watch_s) and a videos DataFrame (video_id, duration_s), compute completion rate and median watched fraction for each duration bucket (0-15s, 15-30s, 30-60s, 1-3m, 3m+), counting looped replays correctly.

TikTok
Data Manipulation (SQL/Python)
Data Scientist
TikTok
November 29, 2026
Data Scientist
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


Given a views DataFrame (video_id, watch_s) and a videos DataFrame (video_id, duration_s), compute completion rate and median watched fraction for each duration bucket (0-15s, 15-30s, 30-60s, 1-3m, 3m+), counting looped replays correctly.

TikTok allows uploads from a few seconds to many minutes, and completion rate is a strong ranking signal, so analysts often need to compare it fairly across lengths. This pandas screen checks joins, binning with pd.cut, handling looped playback where watch time exceeds duration, and empty buckets.

What the Interviewer Expects
  • Join views to video metadata and decide what happens to unmatched rows
  • Handle zero or missing durations explicitly
  • Cap watched fraction at 1.0 because short videos loop
  • Bin with pd.cut and keep empty buckets visible
  • Explain why completion rate is not comparable across buckets without normalization
Key Topics to Cover
pandas merge
pd.cut binning
groupby agg
loop playback
completion rate
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you build a duration-adjusted completion score for ranking?
  • How would you compute this per creator and flag creators far below their bucket median?
  • The views table has 2 billion rows. How would you do this without loading it into pandas?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data
  • views(video_id, watch_s): one row per view, watch_s can exceed duration because TikTok auto-loops.
  • videos(video_id, duration_s): metadata; a few rows have duration_s = 0 from failed transc...
Approach
  1. Inner join views to videos, drop non-positive durations.
  2. watch_frac = min(watch_s / duration_s, 1.0) so a 10-second video watched three times counts once, not 300%.
  3. Bucket by duration with ...

Submit Your Answer
Markdown supported