Immediate Food Delivery III - Problem
Immediate Food Delivery III
You're working as a data analyst for a popular food delivery platform! ๐๐
The company wants to understand their delivery patterns by analyzing what percentage of orders are immediate versus scheduled on each day.
Problem Setup:
You have a
Key Definitions:
โข Immediate Order: When
โข Scheduled Order: When
Your Task:
Calculate the percentage of immediate orders for each unique
Expected Output Format:
You're working as a data analyst for a popular food delivery platform! ๐๐
The company wants to understand their delivery patterns by analyzing what percentage of orders are immediate versus scheduled on each day.
Problem Setup:
You have a
Delivery table with the following structure:| Column Name | Type |
|---|---|
| delivery_id | int |
| customer_id | int |
| order_date | date |
| customer_pref_delivery_date | date |
Key Definitions:
โข Immediate Order: When
customer_pref_delivery_date = order_dateโข Scheduled Order: When
customer_pref_delivery_date > order_dateYour Task:
Calculate the percentage of immediate orders for each unique
order_date, rounded to 2 decimal places. Return results ordered by order_date ascending.Expected Output Format:
order_date | immediate_percentage Input & Output
example_1.sql โ Basic Example
$
Input:
Delivery 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 |
| 7 | 4 | 2019-08-09 | 2019-08-09 |
โบ
Output:
| order_date | immediate_percentage |
|------------|---------------------|
| 2019-08-01 | 0.00 |
| 2019-08-02 | 100.00 |
| 2019-08-09 | 100.00 |
| 2019-08-11 | 0.00 |
| 2019-08-21 | 0.00 |
| 2019-08-24 | 100.00 |
๐ก Note:
For 2019-08-01: 0 immediate out of 1 total = 0%. For 2019-08-02: 1 immediate out of 1 total = 100%. For 2019-08-09: 1 immediate out of 1 total = 100%. For 2019-08-11: 0 immediate out of 2 total = 0%. For 2019-08-21: 0 immediate out of 1 total = 0%. For 2019-08-24: 1 immediate out of 1 total = 100%.
example_2.sql โ Mixed Percentages
$
Input:
Delivery table:
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
|-------------|-------------|------------|----------------------------|
| 1 | 1 | 2020-01-01 | 2020-01-01 |
| 2 | 2 | 2020-01-01 | 2020-01-02 |
| 3 | 3 | 2020-01-01 | 2020-01-01 |
| 4 | 4 | 2020-01-02 | 2020-01-03 |
| 5 | 5 | 2020-01-02 | 2020-01-02 |
| 6 | 6 | 2020-01-02 | 2020-01-04 |
โบ
Output:
| order_date | immediate_percentage |
|------------|---------------------|
| 2020-01-01 | 66.67 |
| 2020-01-02 | 33.33 |
๐ก Note:
For 2020-01-01: 2 immediate orders (delivery_id 1,3) out of 3 total = 66.67%. For 2020-01-02: 1 immediate order (delivery_id 5) out of 3 total = 33.33%.
example_3.sql โ Edge Case: All Scheduled
$
Input:
Delivery table:
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
|-------------|-------------|------------|----------------------------|
| 1 | 1 | 2023-12-25 | 2023-12-26 |
| 2 | 2 | 2023-12-25 | 2023-12-27 |
| 3 | 3 | 2023-12-26 | 2023-12-26 |
โบ
Output:
| order_date | immediate_percentage |
|------------|---------------------|
| 2023-12-25 | 0.00 |
| 2023-12-26 | 100.00 |
๐ก Note:
For 2023-12-25: 0 immediate out of 2 total = 0.00% (both orders scheduled for later). For 2023-12-26: 1 immediate out of 1 total = 100.00%.
Visualization
Tap to expand
Understanding the Visualization
1
Data Collection
Orders flow in throughout each day with delivery preferences
2
Daily Grouping
Group all orders by their order date using GROUP BY
3
Conditional Counting
Use CASE statements to count immediate vs total orders simultaneously
4
Percentage Calculation
Calculate and round percentages for each day
5
Report Generation
Sort by date and generate the final analytics report
Key Takeaway
๐ฏ Key Insight: GROUP BY with conditional aggregation (CASE WHEN) allows us to count different types of records and calculate percentages in a single, efficient database operation.
Time & Space Complexity
Time Complexity
O(n)
Single pass through all n records, with GROUP BY typically O(n) with hash-based grouping
โ Linear Growth
Space Complexity
O(d)
Space for d unique dates in GROUP BY result and hash table
โ Linear Space
Constraints
- 1 โค delivery_id โค 105
- 1 โค customer_id โค 104
- order_date and customer_pref_delivery_date are valid dates
- customer_pref_delivery_date โฅ order_date (preferred delivery cannot be before order date)
- All delivery_id values are unique
- Result percentages must be rounded to exactly 2 decimal places
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code