Restaurant Growth - Problem
Restaurant Growth Analysis
You are the owner of a popular restaurant and want to analyze your business growth for potential expansion. Your restaurant has been busy enough to guarantee at least one customer every day.
Given a
Table Schema:
Requirements:
You are the owner of a popular restaurant and want to analyze your business growth for potential expansion. Your restaurant has been busy enough to guarantee at least one customer every day.
Given a
Customer table with transaction data, you need to calculate the 7-day moving average of daily revenue. For each day, compute the average amount paid by customers over a 7-day window (current day + 6 previous days).Table Schema:
| Column Name | Type |
|---|---|
| customer_id | int |
| name | varchar |
| visited_on | date |
| amount | int |
Requirements:
- Primary key:
(customer_id, visited_on) - Calculate moving average for 7-day windows
- Round
average_amountto 2 decimal places - Return results ordered by
visited_onascending - Only include dates that have a complete 7-day window
Input & Output
example_1.sql โ Basic Restaurant Data
$
Input:
Customer table:
| customer_id | name | visited_on | amount |
|-------------|--------------|--------------|--------|
| 1 | Jhon | 2019-01-01 | 100 |
| 2 | Daniel | 2019-01-02 | 110 |
| 3 | Jade | 2019-01-03 | 120 |
| 4 | Khaled | 2019-01-04 | 130 |
| 5 | Winston | 2019-01-05 | 110 |
| 6 | Elvis | 2019-01-06 | 140 |
| 7 | Anna | 2019-01-07 | 150 |
| 8 | Maria | 2019-01-08 | 80 |
| 9 | Jaze | 2019-01-09 | 110 |
โบ
Output:
| visited_on | amount | average_amount |
|--------------|--------|----------------|
| 2019-01-07 | 860 | 122.86 |
| 2019-01-08 | 840 | 120.00 |
| 2019-01-09 | 840 | 120.00 |
๐ก Note:
Day 7 is the first day with a complete 7-day window (days 1-7). The amount is 100+110+120+130+110+140+150=860, and average is 860/7=122.86. For day 8, we exclude day 1 and include day 8: 110+120+130+110+140+150+80=840, average=120.00.
example_2.sql โ Multiple Customers Same Day
$
Input:
Customer table:
| customer_id | name | visited_on | amount |
|-------------|---------|--------------|--------|
| 1 | Alice | 2019-01-01 | 50 |
| 2 | Bob | 2019-01-01 | 75 |
| 3 | Carol | 2019-01-02 | 100 |
| 4 | David | 2019-01-03 | 80 |
| 5 | Eve | 2019-01-04 | 90 |
| 6 | Frank | 2019-01-05 | 60 |
| 7 | Grace | 2019-01-06 | 70 |
| 8 | Henry | 2019-01-07 | 85 |
โบ
Output:
| visited_on | amount | average_amount |
|--------------|--------|----------------|
| 2019-01-07 | 510 | 72.86 |
๐ก Note:
Multiple customers on the same day are aggregated. Day 1 total: 50+75=125. The 7-day window sum is 125+100+80+90+60+70+85=510, giving an average of 72.86.
example_3.sql โ Minimum Data Set
$
Input:
Customer table:
| customer_id | name | visited_on | amount |
|-------------|-------|--------------|--------|
| 1 | John | 2019-01-01 | 100 |
| 2 | Jane | 2019-01-02 | 200 |
| 3 | Bob | 2019-01-03 | 300 |
| 4 | Alice | 2019-01-04 | 400 |
| 5 | Carol | 2019-01-05 | 500 |
| 6 | Dave | 2019-01-06 | 600 |
| 7 | Eve | 2019-01-07 | 700 |
โบ
Output:
| visited_on | amount | average_amount |
|--------------|--------|----------------|
| 2019-01-07 | 2800 | 400.00 |
๐ก Note:
This shows the minimum case where we have exactly 7 days of data. Only day 7 appears in results with sum=100+200+300+400+500+600+700=2800 and average=400.00.
Visualization
Tap to expand
Understanding the Visualization
1
Daily Aggregation
First, sum up all customer payments for each day to get daily totals
2
Sliding Window
For each day, look back 6 days plus current day to create 7-day windows
3
Moving Average
Calculate sum and average for each window, revealing growth trends
Key Takeaway
๐ฏ Key Insight: Window functions eliminate the need for expensive subqueries by calculating sliding windows in a single, optimized database operation.
Time & Space Complexity
Time Complexity
O(n log n)
O(n log n) for sorting by date, then O(n) for window function calculation
โก Linearithmic
Space Complexity
O(n)
Space for daily aggregation and result set
โก Linearithmic Space
Constraints
- 1 โค customer_id โค 1000
- 1 โค amount โค 1000
- At least one customer visits the restaurant every day
- visited_on dates are within a reasonable range
- Primary key constraint: (customer_id, visited_on) is unique
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code