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: restaurant_orders
+------------------+----------+ | Column Name | Type | +------------------+----------+ | order_id | int | | customer_id | int | | order_timestamp | datetime | | order_amount | decimal | | payment_method | varchar | | order_rating | int | +------------------+----------+ order_id is the unique identifier for this table. payment_method can be cash, card, or app. order_rating is between 1 and 5, where 5 is the best (NULL if not rated). order_timestamp contains both date and time information.
Write a solution to find golden hour customers - customers who consistently order during peak hours and provide high satisfaction. A customer is a golden hour customer if they meet ALL the following criteria:
3 orders.60% of their orders are during peak hours (11:00-14:00 or 18:00-21:00).4.0, round it to 2 decimal places.50% of their orders.Return the result table ordered by average_rating in descending order, then by customer_id in descending order.
The result format is in the following example.
Example:
Input:
restaurant_orders table:
+----------+-------------+---------------------+--------------+----------------+--------------+ | order_id | customer_id | order_timestamp | order_amount | payment_method | order_rating | +----------+-------------+---------------------+--------------+----------------+--------------+ | 1 | 101 | 2024-03-01 12:30:00 | 25.50 | card | 5 | | 2 | 101 | 2024-03-02 19:15:00 | 32.00 | app | 4 | | 3 | 101 | 2024-03-03 13:45:00 | 28.75 | card | 5 | | 4 | 101 | 2024-03-04 20:30:00 | 41.00 | app | NULL | | 5 | 102 | 2024-03-01 11:30:00 | 18.50 | cash | 4 | | 6 | 102 | 2024-03-02 12:00:00 | 22.00 | card | 3 | | 7 | 102 | 2024-03-03 15:30:00 | 19.75 | cash | NULL | | 8 | 103 | 2024-03-01 19:00:00 | 55.00 | app | 5 | | 9 | 103 | 2024-03-02 20:45:00 | 48.50 | app | 4 | | 10 | 103 | 2024-03-03 18:30:00 | 62.00 | card | 5 | | 11 | 104 | 2024-03-01 10:00:00 | 15.00 | cash | 3 | | 12 | 104 | 2024-03-02 09:30:00 | 18.00 | cash | 2 | | 13 | 104 | 2024-03-03 16:00:00 | 20.00 | card | 3 | | 14 | 105 | 2024-03-01 12:15:00 | 30.00 | app | 4 | | 15 | 105 | 2024-03-02 13:00:00 | 35.50 | app | 5 | | 16 | 105 | 2024-03-03 11:45:00 | 28.00 | card | 4 | +----------+-------------+---------------------+--------------+----------------+--------------+
Output:
+-------------+--------------+----------------------+----------------+ | customer_id | total_orders | peak_hour_percentage | average_rating | +-------------+--------------+----------------------+----------------+ | 103 | 3 | 100 | 4.67 | | 101 | 4 | 100 | 4.67 | | 105 | 3 | 100 | 4.33 | +-------------+--------------+----------------------+----------------+
Explanation:
The results table is ordered by average_rating DESC, then customer_id DESC.
Problem summary: Table: restaurant_orders +------------------+----------+ | Column Name | Type | +------------------+----------+ | order_id | int | | customer_id | int | | order_timestamp | datetime | | order_amount | decimal | | payment_method | varchar | | order_rating | int | +------------------+----------+ order_id is the unique identifier for this table. payment_method can be cash, card, or app. order_rating is between 1 and 5, where 5 is the best (NULL if not rated). order_timestamp contains both date and time information. Write a solution to find golden hour customers - customers who consistently order during peak hours and provide high satisfaction. A customer is a golden hour customer if they meet ALL the following criteria: Made at least 3 orders. At least 60% of their orders are during peak hours (11:00-14:00 or 18:00-21:00). Their average rating for rated orders is at least 4.0, round it to
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"headers":{"restaurant_orders":["order_id","customer_id","order_timestamp","order_amount","payment_method","order_rating"]},"rows":{"restaurant_orders":[[1,101,"2024-03-01 12:30:00",25.50,"card",5],[2,101,"2024-03-02 19:15:00",32.00,"app",4],[3,101,"2024-03-03 13:45:00",28.75,"card",5],[4,101,"2024-03-04 20:30:00",41.00,"app",null],[5,102,"2024-03-01 11:30:00",18.50,"cash",4],[6,102,"2024-03-02 12:00:00",22.00,"card",3],[7,102,"2024-03-03 15:30:00",19.75,"cash",null],[8,103,"2024-03-01 19:00:00",55.00,"app",5],[9,103,"2024-03-02 20:45:00",48.50,"app",4],[10,103,"2024-03-03 18:30:00",62.00,"card",5],[11,104,"2024-03-01 10:00:00",15.00,"cash",3],[12,104,"2024-03-02 09:30:00",18.00,"cash",2],[13,104,"2024-03-03 16:00:00",20.00,"card",3],[14,105,"2024-03-01 12:15:00",30.00,"app",4],[15,105,"2024-03-02 13:00:00",35.50,"app",5],[16,105,"2024-03-03 11:45:00",28.00,"card",4]]}}Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3705: Find Golden Hour Customers
// Auto-generated Java example from py.
class Solution {
public void exampleSolution() {
}
}
// Reference (py):
// # Accepted solution for LeetCode #3705: Find Golden Hour Customers
// import pandas as pd
// import numpy as np
//
//
// def find_golden_hour_customers(restaurant_orders: pd.DataFrame) -> pd.DataFrame:
// df = restaurant_orders.copy()
// df["order_timestamp"] = pd.to_datetime(df["order_timestamp"])
// df["is_peak_hour"] = df["order_timestamp"].dt.time.between(
// pd.to_datetime("11:00:00").time(), pd.to_datetime("14:00:00").time()
// ) | df["order_timestamp"].dt.time.between(
// pd.to_datetime("18:00:00").time(), pd.to_datetime("21:00:00").time()
// )
// grouped = (
// df.groupby("customer_id")
// .agg(
// total_orders=("order_timestamp", "count"),
// peak_hour_count=("is_peak_hour", "sum"),
// average_rating=("order_rating", lambda x: x.dropna().mean()),
// non_null_rating_count=("order_rating", lambda x: x.notna().sum()),
// )
// .reset_index()
// )
// grouped["average_rating"] = grouped["average_rating"].round(2)
// grouped["peak_hour_percentage"] = (
// grouped["peak_hour_count"] / grouped["total_orders"] * 100
// ).round()
// filtered = grouped[
// (grouped["total_orders"] >= 3)
// & (grouped["peak_hour_percentage"] >= 60)
// & (grouped["average_rating"] >= 4.0)
// & (grouped["non_null_rating_count"] / grouped["total_orders"] >= 0.5)
// ]
// filtered = filtered.sort_values(
// by=["average_rating", "customer_id"], ascending=[False, False]
// )
// return filtered[
// ["customer_id", "total_orders", "peak_hour_percentage", "average_rating"]
// ]
// Accepted solution for LeetCode #3705: Find Golden Hour Customers
// Auto-generated Go example from py.
func exampleSolution() {
}
// Reference (py):
// # Accepted solution for LeetCode #3705: Find Golden Hour Customers
// import pandas as pd
// import numpy as np
//
//
// def find_golden_hour_customers(restaurant_orders: pd.DataFrame) -> pd.DataFrame:
// df = restaurant_orders.copy()
// df["order_timestamp"] = pd.to_datetime(df["order_timestamp"])
// df["is_peak_hour"] = df["order_timestamp"].dt.time.between(
// pd.to_datetime("11:00:00").time(), pd.to_datetime("14:00:00").time()
// ) | df["order_timestamp"].dt.time.between(
// pd.to_datetime("18:00:00").time(), pd.to_datetime("21:00:00").time()
// )
// grouped = (
// df.groupby("customer_id")
// .agg(
// total_orders=("order_timestamp", "count"),
// peak_hour_count=("is_peak_hour", "sum"),
// average_rating=("order_rating", lambda x: x.dropna().mean()),
// non_null_rating_count=("order_rating", lambda x: x.notna().sum()),
// )
// .reset_index()
// )
// grouped["average_rating"] = grouped["average_rating"].round(2)
// grouped["peak_hour_percentage"] = (
// grouped["peak_hour_count"] / grouped["total_orders"] * 100
// ).round()
// filtered = grouped[
// (grouped["total_orders"] >= 3)
// & (grouped["peak_hour_percentage"] >= 60)
// & (grouped["average_rating"] >= 4.0)
// & (grouped["non_null_rating_count"] / grouped["total_orders"] >= 0.5)
// ]
// filtered = filtered.sort_values(
// by=["average_rating", "customer_id"], ascending=[False, False]
// )
// return filtered[
// ["customer_id", "total_orders", "peak_hour_percentage", "average_rating"]
// ]
# Accepted solution for LeetCode #3705: Find Golden Hour Customers
import pandas as pd
import numpy as np
def find_golden_hour_customers(restaurant_orders: pd.DataFrame) -> pd.DataFrame:
df = restaurant_orders.copy()
df["order_timestamp"] = pd.to_datetime(df["order_timestamp"])
df["is_peak_hour"] = df["order_timestamp"].dt.time.between(
pd.to_datetime("11:00:00").time(), pd.to_datetime("14:00:00").time()
) | df["order_timestamp"].dt.time.between(
pd.to_datetime("18:00:00").time(), pd.to_datetime("21:00:00").time()
)
grouped = (
df.groupby("customer_id")
.agg(
total_orders=("order_timestamp", "count"),
peak_hour_count=("is_peak_hour", "sum"),
average_rating=("order_rating", lambda x: x.dropna().mean()),
non_null_rating_count=("order_rating", lambda x: x.notna().sum()),
)
.reset_index()
)
grouped["average_rating"] = grouped["average_rating"].round(2)
grouped["peak_hour_percentage"] = (
grouped["peak_hour_count"] / grouped["total_orders"] * 100
).round()
filtered = grouped[
(grouped["total_orders"] >= 3)
& (grouped["peak_hour_percentage"] >= 60)
& (grouped["average_rating"] >= 4.0)
& (grouped["non_null_rating_count"] / grouped["total_orders"] >= 0.5)
]
filtered = filtered.sort_values(
by=["average_rating", "customer_id"], ascending=[False, False]
)
return filtered[
["customer_id", "total_orders", "peak_hour_percentage", "average_rating"]
]
// Accepted solution for LeetCode #3705: Find Golden Hour Customers
// Rust example auto-generated from py reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (py):
// # Accepted solution for LeetCode #3705: Find Golden Hour Customers
// import pandas as pd
// import numpy as np
//
//
// def find_golden_hour_customers(restaurant_orders: pd.DataFrame) -> pd.DataFrame:
// df = restaurant_orders.copy()
// df["order_timestamp"] = pd.to_datetime(df["order_timestamp"])
// df["is_peak_hour"] = df["order_timestamp"].dt.time.between(
// pd.to_datetime("11:00:00").time(), pd.to_datetime("14:00:00").time()
// ) | df["order_timestamp"].dt.time.between(
// pd.to_datetime("18:00:00").time(), pd.to_datetime("21:00:00").time()
// )
// grouped = (
// df.groupby("customer_id")
// .agg(
// total_orders=("order_timestamp", "count"),
// peak_hour_count=("is_peak_hour", "sum"),
// average_rating=("order_rating", lambda x: x.dropna().mean()),
// non_null_rating_count=("order_rating", lambda x: x.notna().sum()),
// )
// .reset_index()
// )
// grouped["average_rating"] = grouped["average_rating"].round(2)
// grouped["peak_hour_percentage"] = (
// grouped["peak_hour_count"] / grouped["total_orders"] * 100
// ).round()
// filtered = grouped[
// (grouped["total_orders"] >= 3)
// & (grouped["peak_hour_percentage"] >= 60)
// & (grouped["average_rating"] >= 4.0)
// & (grouped["non_null_rating_count"] / grouped["total_orders"] >= 0.5)
// ]
// filtered = filtered.sort_values(
// by=["average_rating", "customer_id"], ascending=[False, False]
// )
// return filtered[
// ["customer_id", "total_orders", "peak_hour_percentage", "average_rating"]
// ]
// Accepted solution for LeetCode #3705: Find Golden Hour Customers
// Auto-generated TypeScript example from py.
function exampleSolution(): void {
}
// Reference (py):
// # Accepted solution for LeetCode #3705: Find Golden Hour Customers
// import pandas as pd
// import numpy as np
//
//
// def find_golden_hour_customers(restaurant_orders: pd.DataFrame) -> pd.DataFrame:
// df = restaurant_orders.copy()
// df["order_timestamp"] = pd.to_datetime(df["order_timestamp"])
// df["is_peak_hour"] = df["order_timestamp"].dt.time.between(
// pd.to_datetime("11:00:00").time(), pd.to_datetime("14:00:00").time()
// ) | df["order_timestamp"].dt.time.between(
// pd.to_datetime("18:00:00").time(), pd.to_datetime("21:00:00").time()
// )
// grouped = (
// df.groupby("customer_id")
// .agg(
// total_orders=("order_timestamp", "count"),
// peak_hour_count=("is_peak_hour", "sum"),
// average_rating=("order_rating", lambda x: x.dropna().mean()),
// non_null_rating_count=("order_rating", lambda x: x.notna().sum()),
// )
// .reset_index()
// )
// grouped["average_rating"] = grouped["average_rating"].round(2)
// grouped["peak_hour_percentage"] = (
// grouped["peak_hour_count"] / grouped["total_orders"] * 100
// ).round()
// filtered = grouped[
// (grouped["total_orders"] >= 3)
// & (grouped["peak_hour_percentage"] >= 60)
// & (grouped["average_rating"] >= 4.0)
// & (grouped["non_null_rating_count"] / grouped["total_orders"] >= 0.5)
// ]
// filtered = filtered.sort_values(
// by=["average_rating", "customer_id"], ascending=[False, False]
// )
// return filtered[
// ["customer_id", "total_orders", "peak_hour_percentage", "average_rating"]
// ]
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.