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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
Compute Session Durations and Gaps
00:00
5 left

Compute Session Durations and Gaps

HardSQL · PostgreSQL

Problem

Adobe Analytics stores user session logs with a start and end timestamp for each session. Write a SQL query to calculate the duration of each session and identify the gap from the previous session for the same user.

Requirements

  1. Return one row per session.
  2. Calculate session_duration_minutes as the difference between session_end and session_start.
  3. Calculate gap_from_previous_minutes as the time between the current session start and the previous session end for the same user.
  4. Add gap_flag with value TRUE when the gap is greater than 30 minutes, otherwise FALSE.
  5. Order the result by user_id, then session_start.

Output details

  • Return columns in this order: user_id, session_id, session_start, session_end, session_duration_minutes, gap_from_previous_minutes, gap_flag.
  • Sort user_id and session_start in ascending order.
  • Return NULL for gap_from_previous_minutes when no previous session exists, and FALSE for gap_flag.
  • Preserve full numeric precision without rounding session_duration_minutes or gap_from_previous_minutes.

Schema

session_logs
ColumnTypeDescription
session_idPKINTPrimary key for the session record
user_idINTAdobe user identifier
session_startTIMESTAMPSession start time
session_endTIMESTAMPSession end time
device_typeVARCHAR(50)Device used for the session
Tablessession_logs
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the one table are in the Question tab.

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.