Find Peak Calling Hours for Each City - Problem
Find Peak Calling Hours for Each City
You're tasked with analyzing call data from a telecommunications company to identify peak calling hours for each city in their network. Given a table of call records, you need to determine which hour(s) of the day have the highest call volume for each city.
The Challenge:
- Extract the hour from each call's timestamp
- Count calls per hour for each city
- Find the hour(s) with maximum calls (there can be ties!)
- Return results ordered by peak hour and city in descending order
Table Structure:
•
•
•
•
Key Points:
• Multiple hours can be peak hours if they have the same call count
• Results must be ordered by peak_hour DESC, city DESC
• Use
You're tasked with analyzing call data from a telecommunications company to identify peak calling hours for each city in their network. Given a table of call records, you need to determine which hour(s) of the day have the highest call volume for each city.
The Challenge:
- Extract the hour from each call's timestamp
- Count calls per hour for each city
- Find the hour(s) with maximum calls (there can be ties!)
- Return results ordered by peak hour and city in descending order
Table Structure:
Calls table contains:•
caller_id (int) - ID of the caller•
recipient_id (int) - ID of the recipient•
call_time (datetime) - When the call was made•
city (varchar) - City where the call originatedKey Points:
• Multiple hours can be peak hours if they have the same call count
• Results must be ordered by peak_hour DESC, city DESC
• Use
HOUR(call_time) to extract hour from datetime Input & Output
example_1.sql — Basic Peak Hour Detection
$
Input:
Calls table:
+----------+--------------+---------------------+--------+
| caller_id| recipient_id | call_time | city |
+----------+--------------+---------------------+--------+
| 1 | 2 | 2023-07-17 13:30:00 | Berlin |
| 2 | 1 | 2023-07-17 13:45:00 | Berlin |
| 3 | 4 | 2023-07-17 14:15:00 | Berlin |
| 4 | 3 | 2023-07-17 15:20:00 | London |
| 5 | 6 | 2023-07-17 15:35:00 | London |
+----------+--------------+---------------------+--------+
›
Output:
+--------+-----------+
| city | peak_hour |
+--------+-----------+
| London | 15 |
| Berlin | 13 |
+--------+-----------+
💡 Note:
Berlin has 2 calls at hour 13 and 1 call at hour 14, so peak hour is 13. London has 2 calls at hour 15, making it the peak. Results are ordered by peak_hour DESC (15, 13), then by city DESC (London, Berlin).
example_2.sql — Multiple Peak Hours (Ties)
$
Input:
Calls table:
+----------+--------------+---------------------+----------+
| caller_id| recipient_id | call_time | city |
+----------+--------------+---------------------+----------+
| 1 | 2 | 2023-07-17 09:00:00 | New York |
| 2 | 3 | 2023-07-17 09:30:00 | New York |
| 3 | 4 | 2023-07-17 17:00:00 | New York |
| 4 | 5 | 2023-07-17 17:45:00 | New York |
| 5 | 6 | 2023-07-17 10:15:00 | Chicago |
+----------+--------------+---------------------+----------+
›
Output:
+----------+-----------+
| city | peak_hour |
+----------+-----------+
| New York | 17 |
| Chicago | 10 |
| New York | 9 |
+----------+-----------+
💡 Note:
New York has 2 calls each at hours 9 and 17 (tied for peak), Chicago has 1 call at hour 10. All peak hours are included. Ordered by peak_hour DESC (17, 10, 9), then city DESC within same hour.
example_3.sql — Single Call Per City
$
Input:
Calls table:
+----------+--------------+---------------------+-------+
| caller_id| recipient_id | call_time | city |
+----------+--------------+---------------------+-------+
| 1 | 2 | 2023-07-17 08:00:00 | Paris |
| 3 | 4 | 2023-07-17 12:00:00 | Tokyo |
| 5 | 6 | 2023-07-17 20:00:00 | Dubai |
+----------+--------------+---------------------+-------+
›
Output:
+-------+-----------+
| city | peak_hour |
+-------+-----------+
| Dubai | 20 |
| Tokyo | 12 |
| Paris | 8 |
+-------+-----------+
💡 Note:
Each city has only one call, so each hour is a peak hour for its respective city. Results ordered by peak_hour DESC (20, 12, 8), then by city DESC.
Constraints
- 1 ≤ number of calls ≤ 104
- call_time is a valid datetime
- city is a non-empty varchar
- Primary key constraint: (caller_id, recipient_id, call_time) is unique
- Hours range from 0 to 23 (24-hour format)
Visualization
Tap to expand
Understanding the Visualization
1
Data Collection
Gather all call records with timestamps and city information
2
Hour Extraction
Extract hour (0-23) from each call timestamp using HOUR() function
3
City Grouping
Group calls by city and hour, count total calls for each combination
4
Peak Detection
Use window functions to find maximum call count within each city
5
Result Filtering
Filter to show only hours that match the peak count for each city
Key Takeaway
🎯 Key Insight: Window functions with PARTITION BY enable us to calculate city-specific maximums while maintaining row-level detail for filtering, making this a single-pass optimal solution.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code