Who Holds Attention

A hard Spark interview practice problem on DataDriven. Write the Spark code and run it against the tables, with instant feedback.

Domain
Spark
Difficulty
hard
Seniority
mid
Asked in
TikTok

Problem

Our short-video platform keeps a small video catalog in content_items while the view log in content_views grows fast, so the creator team wants the catalog broadcast to the views rather than the view log moved for the lookup. Pair each view with its video's creator and report each creator's total seconds watched and number of views, keeping only creators whose videos drew more than 1000 seconds in total. Videos with no creator on record count together as one row with a blank creator, and the creators can come back in any order.

Tables

content_views

Columns: view_id, content_id, user_id, device_id, viewed_at, watch_seconds

content_views: 10 of 200 rows
view_idcontent_iduser_iddevice_idviewed_atwatch_seconds
705337213614042026-02-02 01:07:0041
710650126226072026-03-03 02:14:0072
715963038838102026-04-04 03:21:00103
7212759514410132026-05-05 04:28:00134
7265888640512162026-06-06 05:35:00165
73181017766614192026-07-07 06:42:00196
73711146NULL16222026-08-08 07:49:00227
7424127548818252026-09-09 08:56:00258
747714041749NULL2026-01-10 09:03:00289
75301533301022312026-02-11 10:10:00320

content_items

Columns: content_id, title, content_type, duration_seconds, creator_id, publish_date

content_items: 10 of 200 rows
content_idtitlecontent_typeduration_secondscreator_idpublish_date
243The Ultimate Guide to AI TrendsarticleNULL1972026-02-02
286Top 10 Mobile Appspodcast1542942025-03-03
329Understanding Machine Learning [DRAFT]short2013912026-04-04
372Mastering Tech Startupslivestream2484882025-05-05
415Introduction to Cybersecurityvideo2955852026-06-06
458Deep Dive: Cloud ComputingarticleNULL6822025-07-07
501Behind the Scenes of Product Managementpodcast3897792026-08-08
544Breaking Down DevOpsshort4368762025-09-09
587Exploring Web Developmentlivestream483NULL2026-01-10
630How to Data Sciencevideo53010702025-11-11

Expected output

First 5 of 46 rows
creator_idtotal_watch_secondsview_count
NULL3052826
10036162
19746702
29484414
391116547

Worked solution and explanation

This is a fact-into-dimension join wearing a creator-analytics costume, and it has two places to lose. The logical one: the 1000-second bar belongs to a creator's summed watch time, so a filter on individual views throws away every modest view before it can count, and creators whose many short views add up simply vanish. The physical one: the catalog is a tiny dimension that just carries content_id to creator_id, yet a default join keys both sides by content_id and drags the whole view log across the network to meet it. Broadcast the catalog and the fact never moves. Miss that and your one cheap join becomes the most expensive stage in the job.


The two shuffles, and which one you can kill

Step 1: Broadcast the catalog, not the view log

content_items is small enough to fit in executor memory, so wrap it in F.broadcast(). Spark ships one copy to every executor and joins locally against whatever view partitions already live there. No shuffle on the big side. Without the hint, Spark may still pick a sort-merge join and repartition the whole view log by content_id first.

Step 2: Accept the groupBy shuffle, then watch it for skew

Grouping by creator_id is a wide transformation: it has to exchange rows so every creator lands on one partition. That shuffle is unavoidable here, but it is where a viral creator with millions of views melts one task while the rest idle. Knowing the difference between the join shuffle (avoidable) and the aggregation shuffle (intrinsic) is the senior tell.

Step 3: Put the bar on the creator, after the agg

The more-than-1000-seconds rule reads the summed total_watch_seconds, so it cannot run until the aggregation completes. Filter on the aggregated column, never on raw watch_seconds. Skip a final sort as well: the answer is a set of creators, and an orderBy would buy a second exchange the question never asked for.

Bar on each view
views.filter(F.col('watch_seconds') > 1000) before the groupBy keeps only single views longer than 1000 seconds. A creator with 7 views of 400 seconds each has 2800 seconds of watch time and disappears entirely.
Bar on each creator
.filter(F.col('total_watch_seconds') > 1000) after the agg reads the summed column, so every view counts toward its creator before the cut. Same one-line filter, one step later, a different answer.

The job, stage by stage

The broadcast join keeps the fact in place; the only network exchange is the `groupBy`.
  • content_views (fact) (source)
  • content_items (small) (source)
  • broadcast join (transform)
  • groupBy creator_id (transform)
  • post-agg filter (transform)
  • per-creator result (consumer)
  • content_items (small) -> broadcast join
  • content_views (fact) -> broadcast join
  • broadcast join -> groupBy creator_id
  • groupBy creator_id -> post-agg filter
  • post-agg filter -> per-creator result

The solution

Per-creator watch summary
from pyspark.sql import functions as F

views = spark.table('content_views')
catalog = spark.table('content_items')

result = (
    views
    .join(F.broadcast(catalog), 'content_id')
    .groupBy('creator_id')
    .agg(
        F.sum('watch_seconds').alias('total_watch_seconds'),
        F.count('view_id').alias('view_count'),
    )
    .filter(F.col('total_watch_seconds') > 1000)
)

F.broadcast() pins the small catalog; the only shuffle left is the unavoidable groupBy.


Common follow-up questions