Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
Table: Customer
+---------------+---------+ | Column Name | Type | +---------------+---------+ | customer_id | int | | name | varchar | | visited_on | date | | amount | int | +---------------+---------+ In SQL,(customer_id, visited_on) is the primary key for this table. This table contains data about customer transactions in a restaurant. visited_on is the date on which the customer with ID (customer_id) has visited the restaurant. amount is the total paid by a customer.
You are the restaurant owner and you want to analyze a possible expansion (there will be at least one customer every day).
Compute the moving average of how much the customer paid in a seven days window (i.e., current day + 6 days before). average_amount should be rounded to two decimal places.
Return the result table ordered by visited_on in ascending order.
The result format is in the following example.
Example 1:
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 | | 1 | Jhon | 2019-01-10 | 130 | | 3 | Jade | 2019-01-10 | 150 | +-------------+--------------+--------------+-------------+ Output: +--------------+--------------+----------------+ | visited_on | amount | average_amount | +--------------+--------------+----------------+ | 2019-01-07 | 860 | 122.86 | | 2019-01-08 | 840 | 120 | | 2019-01-09 | 840 | 120 | | 2019-01-10 | 1000 | 142.86 | +--------------+--------------+----------------+ Explanation: 1st moving average from 2019-01-01 to 2019-01-07 has an average_amount of (100 + 110 + 120 + 130 + 110 + 140 + 150)/7 = 122.86 2nd moving average from 2019-01-02 to 2019-01-08 has an average_amount of (110 + 120 + 130 + 110 + 140 + 150 + 80)/7 = 120 3rd moving average from 2019-01-03 to 2019-01-09 has an average_amount of (120 + 130 + 110 + 140 + 150 + 80 + 110)/7 = 120 4th moving average from 2019-01-04 to 2019-01-10 has an average_amount of (130 + 110 + 140 + 150 + 80 + 110 + 130 + 150)/7 = 142.86
Problem summary: Table: Customer +---------------+---------+ | Column Name | Type | +---------------+---------+ | customer_id | int | | name | varchar | | visited_on | date | | amount | int | +---------------+---------+ In SQL,(customer_id, visited_on) is the primary key for this table. This table contains data about customer transactions in a restaurant. visited_on is the date on which the customer with ID (customer_id) has visited the restaurant. amount is the total paid by a customer. You are the restaurant owner and you want to analyze a possible expansion (there will be at least one customer every day). Compute the moving average of how much the customer paid in a seven days window (i.e., current day + 6 days before). average_amount should be rounded to two decimal places. Return the result table ordered by visited_on in ascending order. The result format is in the following example.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"headers":{"Customer":["customer_id","name","visited_on","amount"]},"rows":{"Customer":[[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],[1,"Jhon","2019-01-10",130],[3,"Jade","2019-01-10",150]]}}Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1321: Restaurant Growth
// Auto-generated Java example from rust.
class Solution {
public void exampleSolution() {
}
}
// Reference (rust):
// // Accepted solution for LeetCode #1321: Restaurant Growth
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1321: Restaurant Growth
// # Write your MySQL query statement below
// WITH
// t AS (
// SELECT
// visited_on,
// SUM(amount) OVER (
// ORDER BY visited_on
// ROWS 6 PRECEDING
// ) AS amount,
// RANK() OVER (
// ORDER BY visited_on
// ROWS 6 PRECEDING
// ) AS rk
// FROM
// (
// SELECT visited_on, SUM(amount) AS amount
// FROM Customer
// GROUP BY visited_on
// ) AS tt
// )
// SELECT visited_on, amount, ROUND(amount / 7, 2) AS average_amount
// FROM t
// WHERE rk > 6;
// "#
// }
// Accepted solution for LeetCode #1321: Restaurant Growth
// Auto-generated Go example from rust.
func exampleSolution() {
}
// Reference (rust):
// // Accepted solution for LeetCode #1321: Restaurant Growth
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1321: Restaurant Growth
// # Write your MySQL query statement below
// WITH
// t AS (
// SELECT
// visited_on,
// SUM(amount) OVER (
// ORDER BY visited_on
// ROWS 6 PRECEDING
// ) AS amount,
// RANK() OVER (
// ORDER BY visited_on
// ROWS 6 PRECEDING
// ) AS rk
// FROM
// (
// SELECT visited_on, SUM(amount) AS amount
// FROM Customer
// GROUP BY visited_on
// ) AS tt
// )
// SELECT visited_on, amount, ROUND(amount / 7, 2) AS average_amount
// FROM t
// WHERE rk > 6;
// "#
// }
# Accepted solution for LeetCode #1321: Restaurant Growth
# Auto-generated Python example from rust.
def example_solution() -> None:
return
# Reference (rust):
# // Accepted solution for LeetCode #1321: Restaurant Growth
# pub fn sql_example() -> &'static str {
# r#"
# -- Accepted solution for LeetCode #1321: Restaurant Growth
# # Write your MySQL query statement below
# WITH
# t AS (
# SELECT
# visited_on,
# SUM(amount) OVER (
# ORDER BY visited_on
# ROWS 6 PRECEDING
# ) AS amount,
# RANK() OVER (
# ORDER BY visited_on
# ROWS 6 PRECEDING
# ) AS rk
# FROM
# (
# SELECT visited_on, SUM(amount) AS amount
# FROM Customer
# GROUP BY visited_on
# ) AS tt
# )
# SELECT visited_on, amount, ROUND(amount / 7, 2) AS average_amount
# FROM t
# WHERE rk > 6;
# "#
# }
// Accepted solution for LeetCode #1321: Restaurant Growth
pub fn sql_example() -> &'static str {
r#"
-- Accepted solution for LeetCode #1321: Restaurant Growth
# Write your MySQL query statement below
WITH
t AS (
SELECT
visited_on,
SUM(amount) OVER (
ORDER BY visited_on
ROWS 6 PRECEDING
) AS amount,
RANK() OVER (
ORDER BY visited_on
ROWS 6 PRECEDING
) AS rk
FROM
(
SELECT visited_on, SUM(amount) AS amount
FROM Customer
GROUP BY visited_on
) AS tt
)
SELECT visited_on, amount, ROUND(amount / 7, 2) AS average_amount
FROM t
WHERE rk > 6;
"#
}
// Accepted solution for LeetCode #1321: Restaurant Growth
// Auto-generated TypeScript example from rust.
function exampleSolution(): void {
}
// Reference (rust):
// // Accepted solution for LeetCode #1321: Restaurant Growth
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1321: Restaurant Growth
// # Write your MySQL query statement below
// WITH
// t AS (
// SELECT
// visited_on,
// SUM(amount) OVER (
// ORDER BY visited_on
// ROWS 6 PRECEDING
// ) AS amount,
// RANK() OVER (
// ORDER BY visited_on
// ROWS 6 PRECEDING
// ) AS rk
// FROM
// (
// SELECT visited_on, SUM(amount) AS amount
// FROM Customer
// GROUP BY visited_on
// ) AS tt
// )
// SELECT visited_on, amount, ROUND(amount / 7, 2) AS average_amount
// FROM t
// WHERE rk > 6;
// "#
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.