Immediate Food Delivery I - Problem

Table: Delivery

Column NameType
delivery_idint
customer_idint
order_datedate
customer_pref_delivery_datedate

delivery_id is the primary key of this table. The table holds information about food delivery to customers that make orders at some date and specify a preferred delivery date.

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.

Write a solution to find the percentage of immediate orders in the table, 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: Orders are immediate if order_date equals customer_pref_delivery_date

Input & Output

Example 1 — Mixed Immediate and Scheduled 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:

Out of 6 total orders, only orders with delivery_id 2 and 4 are immediate (order_date = customer_pref_delivery_date). So 2/6 = 33.33%.

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
Output:
immediate_percentage
100
💡 Note:

Both orders are immediate since order_date equals customer_pref_delivery_date for all orders. Result is 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-02
2 2 2019-08-02 2019-08-05
Output:
immediate_percentage
0
💡 Note:

No orders are immediate since all have different order_date and customer_pref_delivery_date. Result is 0.00%.

Constraints

  • 1 ≤ delivery_id ≤ 500
  • 1 ≤ customer_id ≤ 100
  • order_date and customer_pref_delivery_date are valid dates
  • The table contains at least one row

Visualization

Tap to expand
Immediate Food Delivery I INPUT: Delivery Table id cust_id order_date pref_date 1 101 2023-01-01 2023-01-01 OK 2 102 2023-01-02 2023-01-03 3 103 2023-01-03 2023-01-03 OK 4 104 2023-01-04 2023-01-06 5 105 2023-01-05 2023-01-05 OK Legend Immediate Order Scheduled Total Orders: 5 Immediate: 3 Scheduled: 2 ALGORITHM STEPS 1 Count Immediate Orders WHERE order_date = pref_date SUM(order_date = pref_date) 2 Count Total Orders COUNT(*) all rows COUNT(*) = 5 3 Calculate Percentage (immediate / total) * 100 (3 / 5) * 100 = 60.00 4 Round to 2 Decimals ROUND(result, 2) ROUND(60.00, 2) = 60.00 AVG(order_date=pref_date)*100 FINAL RESULT 60% 40% Immediate (3) Scheduled (2) Output: 60.00 Percentage of immediate orders rounded to 2 decimals Key Insight: Use boolean comparison (order_date = pref_delivery_date) which returns 1 for TRUE, 0 for FALSE. SUM of these values gives immediate count. Divide by COUNT(*) and multiply by 100 for percentage. Alternative: Use AVG() directly on boolean for cleaner single-expression solution. TutorialsPoint - Immediate Food Delivery I | Optimal Solution
Asked in
DoorDash 15 Uber 8
35.0K Views
Medium Frequency
~8 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