Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Rolling Average of Late Deliveries
00:00
5 left

Rolling Average of Late Deliveries

MediumSQL · PostgreSQL

Problem

HelloFresh operations analysts monitor weekly delivery reliability across fulfillment periods. Write a PostgreSQL query that calculates the percentage of late deliveries for each calendar week and a three-week rolling average of that percentage.

Requirements

  1. Join calendar_weeks to deliveries using each delivery's delivery_date and the week's date range.
  2. Calculate total deliveries, late deliveries, and the late-delivery percentage. A delivery is late when delivered_at is later than promised_at. Treat undelivered records as not yet classifiable and exclude them from the late count, but include them in total deliveries.
  3. Include weeks with no deliveries, reporting a late-delivery rate of 0 for those weeks.
  4. Use a window function to calculate the average late-delivery percentage for the current week and the two preceding weeks. Round percentage values to two decimal places.

Schema

deliveries
ColumnTypeDescription
delivery_idPKINTUnique delivery identifier
delivery_dateDATEDate assigned to the delivery
promised_atTIMESTAMPPromised delivery timestamp
delivered_atTIMESTAMPActual delivery timestamp
delivery_channelVARCHAR(20)Delivery channel
calendar_weeks
ColumnTypeDescription
week_startPKDATEInclusive start of the reporting week
week_endDATEExclusive end of the reporting week
Tablesdeliveriescalendar_weeks
Interviewer

Your question is Rolling Average of Late Deliveries. 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.