Calculate Orders Within Each Interval - Problem

You're analyzing order data for a busy restaurant that tracks the number of orders received every minute throughout the day. Your task is to group these orders into 6-minute intervals and calculate the total orders for each interval.

Database Schema:

Table: Orders
+-------------+------+
| Column Name | Type |
+-------------+------+
| minute      | int  |
| order_count | int  |
+-------------+------+

The minute column is the primary key, representing the specific minute when orders were received. The order_count shows how many orders came in during that minute.

Interval Rules:

  • Minutes 1-6 belong to Interval 1
  • Minutes 7-12 belong to Interval 2
  • Minutes 13-18 belong to Interval 3
  • And so on...

Goal: Write a SQL query that calculates the total orders within each 6-minute interval and returns the results ordered by interval number ascending.

Input & Output

Basic Interval Grouping
$ Input: Orders table: | minute | order_count | |--------|-------------| | 1 | 5 | | 2 | 3 | | 6 | 2 | | 7 | 10 | | 8 | 2 | | 12 | 4 |
Output: | interval_no | total_orders | |-------------|---------------| | 1 | 10 | | 2 | 16 |
💡 Note: Minutes 1, 2, and 6 fall in interval 1 (1-6): 5+3+2=10 orders. Minutes 7, 8, and 12 fall in interval 2 (7-12): 10+2+4=16 orders.
Multiple Intervals
$ Input: Orders table: | minute | order_count | |--------|-------------| | 1 | 2 | | 7 | 4 | | 13 | 6 | | 19 | 8 | | 25 | 10 |
Output: | interval_no | total_orders | |-------------|---------------| | 1 | 2 | | 2 | 4 | | 3 | 6 | | 4 | 8 | | 5 | 10 |
💡 Note: Each minute represents the first minute of its respective 6-minute interval: 1→interval 1, 7→interval 2, 13→interval 3, 19→interval 4, 25→interval 5.
Edge Case - Last Minute of Intervals
$ Input: Orders table: | minute | order_count | |--------|-------------| | 6 | 15 | | 12 | 20 | | 18 | 25 | | 24 | 30 |
Output: | interval_no | total_orders | |-------------|---------------| | 1 | 15 | | 2 | 20 | | 3 | 25 | | 4 | 30 |
💡 Note: Minutes 6, 12, 18, 24 are the last minutes of intervals 1, 2, 3, 4 respectively. The formula (minute + 5) / 6 correctly assigns: 6→1, 12→2, 18→3, 24→4.

Visualization

Tap to expand
🍽️ Restaurant Order Analysis by 6-Minute IntervalsTime Intervals (6 minutes each)Interval 1 (Minutes 1-6)1Min 1: 5 orders2Min 2: 3 orders6Min 6: 2 ordersTotal10Interval 2 (Minutes 7-12)7Min 7: 10 orders8Min 8: 2 orders12Min 12: 4 ordersTotal16Formula Applicationinterval_no = (minute + 5) / 6Examples:Min 1: (1+5)/6 = 6/6 = 1 ✓Min 6: (6+5)/6 = 11/6 = 1 ✓Min 7: (7+5)/6 = 12/6 = 2 ✓Min 12: (12+5)/6 = 17/6 = 2 ✓📊 SQL Query ProcessSELECT (minute + 5) / 6 AS interval_no, SUM(order_count) AS total_ordersFROMOrdersGROUP BY(minute + 5) / 6ORDER BYinterval_no;Result| interval_no | total_orders ||-------------|--------------|| 1 | 10 || 2 | 16 |🎯 Key Insight: Mathematical formula eliminates need for manual range checking!
Understanding the Visualization
1
Collect Orders
Restaurant receives orders every minute with varying quantities
2
Apply Formula
Use (minute + 5) / 6 to determine which 6-minute interval each order belongs to
3
Group & Sum
Combine all orders within the same interval to get totals
4
Analyze Patterns
Review interval totals to identify peak periods and optimize operations
Key Takeaway
🎯 Key Insight: The formula (minute + 5) / 6 elegantly calculates interval numbers using ceiling division, making the solution scalable and efficient for any number of intervals.

Time & Space Complexity

Time Complexity
⏱️
O(n log k)

Single pass through n records with O(log k) grouping operations where k is number of intervals

n
2n
Linearithmic
Space Complexity
O(k)

Storage for k interval groups in the result set

n
2n
Linear Space

Constraints

  • 1 ≤ minute ≤ 104
  • 1 ≤ order_count ≤ 103
  • The total number of rows is a multiple of 6
  • minute is the primary key (no duplicate minutes)
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28
34.2K Views
Medium Frequency
~15 min Avg. Time
890 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