Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Time Gap Between Consecutive Deliveries
00:00
5 left

Time Gap Between Consecutive Deliveries

MediumSQL · PostgreSQL

Problem

Given tables for orders, merchants, and Dashers, write a query using LAG or LEAD to identify the time gap between consecutive deliveries for each driver.

Use completed deliveries with non-null delivery timestamps. Return only deliveries that have a previous delivery for the same Dasher.

Output

  1. One row per delivery with a previous delivery by that Dasher.
  2. Include dasher_id, dasher_name, order_id, merchant_name, delivery_completed_at, previous_delivery_at, and gap_minutes.
  3. Order by dasher_id, delivery_completed_at, and order_id.

Schema

orders
ColumnTypeDescription
order_idPKINTUnique delivery order identifier
merchant_idINTMerchant associated with the order
dasher_idINTDasher assigned to the order
statusVARCHAR(20)Current order status
delivery_completed_atTIMESTAMPTimestamp when the delivery was completed
merchants
ColumnTypeDescription
merchant_idPKINTUnique merchant identifier
merchant_nameVARCHAR(100)Merchant display name
cityVARCHAR(60)Merchant city
dashers
ColumnTypeDescription
dasher_idPKINTUnique Dasher identifier
dasher_nameVARCHAR(100)Dasher name
vehicle_typeVARCHAR(30)Dasher vehicle type
Tablesordersmerchantsdashers
Interviewer

Your question is Time Gap Between Consecutive Deliveries. Start with the requirements and the three 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.