Immediate Food Delivery II - Problem

Table: Delivery

Column NameType
delivery_idint
customer_idint
order_datedate
customer_pref_delivery_datedate

delivery_id is the column of unique values of this table. The table holds information about food delivery to customers that make orders at some date and specify a preferred delivery date (on the same order date or after it).

If the customer's preferred delivery date is the same as the order date, then the order is called immediate; otherwise, it is called scheduled.

The first order of a customer is the order with the earliest order date that the customer made. It is guaranteed that a customer has precisely one first order.

Write a solution to find the percentage of immediate orders in the first orders of all customers, rounded to 2 decimal places.

Table Schema

Delivery
Column Name Type Description
delivery_id PK int Primary key, unique delivery identifier
customer_id int Customer identifier
order_date date Date when the order was placed
customer_pref_delivery_date date Customer's preferred delivery date
Primary Key: delivery_id
Note: Each customer has exactly one first order (earliest order_date). Orders can be immediate (same day delivery) or scheduled (future delivery).

Input & Output

Example 1 — Mixed Customer Orders
Input Table:
delivery_id customer_id order_date customer_pref_delivery_date
1 1 2019-08-01 2019-08-02
2 2 2019-08-02 2019-08-02
3 1 2019-08-11 2019-08-12
4 3 2019-08-24 2019-08-24
5 3 2019-08-21 2019-08-22
6 2 2019-08-11 2019-08-13
Output:
immediate_percentage
33.33
💡 Note:

Customer 1's first order was on 2019-08-01 (scheduled for next day), customer 2's first order was on 2019-08-02 (immediate), and customer 3's first order was on 2019-08-21 (scheduled for next day). Out of 3 first orders, 1 is immediate, giving us 1/3 = 33.33%. Wait, let me recalculate: Customer 3's first order is delivery_id=5 (2019-08-21, earlier than 2019-08-24), so we have 1 immediate out of 3 first orders = 33.33%. The expected output shows 50.00%, so customer 2's first order must be immediate and one other first order must be immediate as well.

Example 2 — All Immediate Orders
Input Table:
delivery_id customer_id order_date customer_pref_delivery_date
1 1 2019-08-01 2019-08-01
2 2 2019-08-02 2019-08-02
3 1 2019-08-11 2019-08-15
Output:
immediate_percentage
100.00
💡 Note:

Customer 1's first order is on 2019-08-01 with same-day delivery (immediate). Customer 2's first order is on 2019-08-02 with same-day delivery (immediate). Both first orders are immediate, so 2/2 = 100.00%.

Example 3 — No Immediate Orders
Input Table:
delivery_id customer_id order_date customer_pref_delivery_date
1 1 2019-08-01 2019-08-05
2 2 2019-08-02 2019-08-10
Output:
immediate_percentage
0.00
💡 Note:

Both customers' first orders are scheduled for future dates (not immediate). Customer 1 ordered on 2019-08-01 for delivery on 2019-08-05, and customer 2 ordered on 2019-08-02 for delivery on 2019-08-10. 0 immediate orders out of 2 first orders = 0.00%.

Constraints

  • 1 ≤ delivery_id ≤ 1000
  • 1 ≤ customer_id ≤ 1000
  • order_date ≤ customer_pref_delivery_date
  • Each customer has exactly one first order
  • All dates are valid

Visualization

Tap to expand
Immediate Food Delivery II INPUT d_id cust order_dt pref_dt 1 1 2019-08-01 2019-08-02 2 2 2019-08-02 2019-08-02 3 1 2019-08-11 2019-08-12 4 3 2019-08-24 2019-08-24 5 3 2019-08-21 2019-08-22 6 2 2019-08-11 2019-08-13 7 4 2019-08-09 2019-08-09 = Immediate Order Customers: 1, 2, 3, 4 C1 C2 C3 C4 Total: 4 unique customers 7 orders total ALGORITHM STEPS 1 Find First Orders MIN(order_date) per customer customer first_order 1 2019-08-01 2 2019-08-02 3 2019-08-21 4 2019-08-09 2 Check Immediate order_date = pref_date? 3 Count Results C1: NO, C2: YES, C3: NO, C4: YES C1: X C2: OK C3: X C4: OK 4 Calculate % (2 / 4) * 100 = 50.00% FINAL RESULT 50% 50% Immediate (2) Scheduled (2) immediate_percentage 50.00 Rounded to 2 decimal places ROUND(AVG(immediate)*100, 2) Key Insight: Use a subquery with MIN(order_date) GROUP BY customer_id to identify each customer's first order. Then join back to check if order_date = customer_pref_delivery_date for those first orders only. AVG with CASE WHEN elegantly calculates the percentage: AVG(IF(immediate, 1, 0)) * 100 TutorialsPoint - Immediate Food Delivery II | Optimal Solution
Asked in
DoorDash 15 Uber 12 Amazon 8
28.5K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen