Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Longest Session and Averages
00:00
5 left

Longest Session and Averages

MediumSQL · PostgreSQL

Problem

Given a table of Roku user streaming sessions, write a query to find the session with the longest duration and calculate the average session length per device type.

Assume duration is calculated from started_at and ended_at. Exclude incomplete or invalid sessions, and break a longest-session tie with the lowest session_id.

Output

  1. One row per device type.
  2. Include device_type, average_session_length_minutes, longest_session_id, longest_session_user_id, longest_session_device_type, and longest_session_length_minutes.
  3. Sort by device_type ascending.

Schema

streaming_sessions
ColumnTypeDescription
session_idPKINTUnique streaming session identifier
user_idINTRoku user identifier
device_type_idINTReferenced Roku device type
started_atTIMESTAMPSession start timestamp
ended_atTIMESTAMPSession end timestamp, null for incomplete sessions
roku_device_types
ColumnTypeDescription
device_type_idPKINTUnique device type identifier
device_typeVARCHAR(80)Readable Roku device type
manufacturerVARCHAR(80)Device manufacturer
Tablesstreaming_sessionsroku_device_types
Interviewer

Your question is Longest Session and Averages. Start with the requirements and the two tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.