Where the Hours Go

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

Domain
Spark
Difficulty
medium
Seniority
mid
Asked in
Synechron

Problem

We're measuring where our streaming viewers' time actually goes, and every session in user_sessions counts its full length in seconds. Total that time by the kind of device each session was watched on, as recorded in devices; that table is small, so broadcast it instead of shuffling the sessions.

Tables

user_sessions

Columns: session_id INTEGER, user_id INTEGER, device_id INTEGER, session_start TEXT, session_duration_sec INTEGER, pages_viewed INTEGER

user_sessions: 10 of 200 rows
session_iduser_iddevice_idsession_startsession_duration_secpages_viewed
70611972302026-02-02 01:00:00533
71222942592026-03-03 02:00:00766
71833912882026-04-04 03:00:00999
72444883172026-05-05 04:00:0012212
73055853462026-06-06 05:00:0014515
73666823752026-07-07 06:00:0016818
74277794042026-08-08 07:00:0019121
7488876NULL2026-09-09 08:00:0021424
75499734622026-01-10 09:00:0023727
761010704912026-02-11 10:00:0026030

devices

Columns: device_id INTEGER, device_type TEXT, os_name TEXT, os_version TEXT, browser TEXT

devices: 10 of 200 rows
device_iddevice_typeos_nameos_versionbrowser
201desktopAndroid15.1Chrome
230tabletWindows16.2Firefox
259smart_tvmacOS17.0Edge
288wearableLinux11Opera
317mobiletvOS12Samsung Internet
346desktopwatchOS13NULL
375tabletiOS10.0.19041Safari
404smart_tvAndroid14.1Chrome
433wearableWindowsNULLFirefox
462mobilemacOS14.0Edge

Expected output

First 5 of 5 rows
device_typetotal_session_seconds
desktop49835
mobile42689
smart_tv48592
tablet45208
wearable43958

Worked solution and explanation

What this is really asking

This is a fact-to-dimension rollup dressed up as an engagement question. The seconds live in user_sessions; the category you report on lives only in devices. Anyone can write a join and a groupBy. What separates candidates is the grain: group by device_id (the key you just joined on) or tack on os_name and browser, and each device type shatters into dozens of rows, so no single total per type exists. The quieter version of the same miss is counting sessions instead of summing session_duration_sec: the shape looks right and every number is wrong.


Build it up

Step 1: Attach each session to its device

session_duration_sec sits in user_sessions, device_type sits in devices. Join on device_id with an inner join: a session with a null or unknown device_id has no type to land in, and an outer join would invent a null device_type row.

Step 2: Group on the attribute, not the key

Group on device_type alone. The join key did its job getting you to the attribute; grouping on it now answers a per-device question nobody asked. Many devices share one type, and the whole point is that their sessions collapse together.

Step 3: Sum the seconds, never count the sessions

F.sum('session_duration_sec') aliased as total_session_seconds. The question is time spent, so a count of sessions is a different metric even though it produces exactly the same five rows.

Grouped too fine
groupBy('device_type', 'os_name') or groupBy('device_id'): one row per device or per OS combo, the same type repeated many times with partial totals.
Grouped at the reported grain
groupBy('device_type') after the join: five rows, one total per type, with every device of that type folded in.

The solution

Total session time per device type
from pyspark.sql import functions as F

result = (
    user_sessions
    .join(F.broadcast(devices), on='device_id', how='inner')
    .groupBy('device_type')
    .agg(F.sum('session_duration_sec').alias('total_session_seconds'))
)

Common follow-up questions