Where Users Linger

An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
easy
Seniority
L3
Asked in
Airbnb

Problem

An analyst sizing up mobile engagement counts only the sessions in user_sessions whose device appears in devices with a mobile device type, crediting each session to that device's operating system. Among those sessions, return the one operating system with the longest average session duration, together with that average.

Attention is the scarcest resource. Find the platform that holds it longest.

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 1 of 1 rows
os_nameavg_duration
macOS1759

Worked solution and explanation

Why this problem exists in real interviews

This is a filtered top-1 dressed up as an engagement question. Anyone can write the join, the AVG and the LIMIT 1. The trap is the cohort: mobile is a column, device_type, not a guess about which os_name values sound like phones. Filter on os_name IN ('iOS','Android') and you drop the macOS rows this data files as mobile, which is exactly the OS that wins, so you hand back a plausible runner-up with a confident average.


Break down the requirements

Step 1: Confirm the mobile predicate

Ask aloud: is device_type = 'mobile' the cohort, or do we treat any os_name of iOS or Android as mobile? The brief says mobile device type, which is the device_type column. Lock it.

Step 2: Join user_sessions to devices

user_sessions us INNER JOIN devices d ON us.device_id = d.device_id. INNER is fine here: a session with no device row has no operating system to credit, so it is data quality noise, not a mobile session.

Step 3: Aggregate average duration

AVG(us.session_duration_sec) grouped by d.os_name. Average across sessions, not users, and versions roll up into one row per OS. Call that out so the interviewer knows you considered the alternative.

Step 4: Sort and cap to one row

ORDER BY avg_duration DESC LIMIT 1. The prompt says the one operating system, singular. Don't build a tie-handling rank unless they push.


The solution

TOP MOBILE OS BY AVG SESSION DURATION
SELECT d.os_name,
       AVG(us.session_duration_sec) AS avg_duration
FROM user_sessions us
INNER JOIN devices d
  ON us.device_id = d.device_id
WHERE d.device_type = 'mobile'
GROUP BY d.os_name
ORDER BY avg_duration DESC
LIMIT 1

COMMON FOLLOW-UP QUESTIONS

Common follow-up questions