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
Total Driver Time Per Day
00:00
5 left

Total Driver Time Per Day

HardSQL · PostgreSQL

Problem

Uber's driver activity reporting needs daily totals even when an Uber trip starts on one calendar day and ends on another. Trip timestamps are stored in UTC. Write a PostgreSQL query that calculates driver time for each day from 2024-01-01 through 2024-01-04, inclusive.

Requirements

  1. Split every valid trip at midnight boundaries, so each calendar day receives only the time occurring on that day.
  2. Clip trips to the reporting period and exclude trips with missing timestamps, nonpositive duration, no matching driver, or no overlap with the period.
  3. Return one row per driver and active date, with total time in hours rounded to two decimal places. Sort by driver name and activity date.

Schema

drivers
ColumnTypeDescription
driver_idPKINTEGERUnique Uber driver identifier
driver_nameVARCHAR(100)Driver display name
trips
ColumnTypeDescription
trip_idPKINTEGERUnique trip identifier
driver_idINTEGERAssigned Uber driver identifier
started_atTIMESTAMPTrip start timestamp in UTC
ended_atTIMESTAMPTrip end timestamp in UTC
Tablesdriverstrips
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables 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.