Find Overlapping Shifts - Problem
Find Overlapping Employee Shifts
You work as a data analyst for a company that needs to identify scheduling conflicts among employees. Given a database table containing employee shift information, your task is to count how many overlapping shifts each employee has worked.
๐ Table: EmployeeShifts
Key Rules:
โข
โข Two shifts overlap if one shift's
โข Count overlapping pairs for each employee
Goal: Return each employee's ID with their total count of overlapping shift pairs, ordered by employee_id ascending.
This problem tests your understanding of interval overlapping logic and SQL aggregation techniques commonly used in workforce management systems.
You work as a data analyst for a company that needs to identify scheduling conflicts among employees. Given a database table containing employee shift information, your task is to count how many overlapping shifts each employee has worked.
๐ Table: EmployeeShifts
+------------------+---------+
| Column Name | Type |
+------------------+---------+
| employee_id | int |
| start_time | time |
| end_time | time |
+------------------+---------+Key Rules:
โข
(employee_id, start_time) is the unique keyโข Two shifts overlap if one shift's
end_time is later than another shift's start_timeโข Count overlapping pairs for each employee
Goal: Return each employee's ID with their total count of overlapping shift pairs, ordered by employee_id ascending.
This problem tests your understanding of interval overlapping logic and SQL aggregation techniques commonly used in workforce management systems.
Input & Output
example_1.py โ Basic Overlap Case
$
Input:
EmployeeShifts:
+-------------+------------+----------+
| employee_id | start_time | end_time |
+-------------+------------+----------+
| 1 | 09:00 | 13:00 |
| 1 | 12:00 | 16:00 |
| 1 | 15:00 | 19:00 |
| 2 | 08:00 | 12:00 |
| 2 | 14:00 | 18:00 |
+-------------+------------+----------+
โบ
Output:
+-------------+---------------+
| employee_id | overlap_count |
+-------------+---------------+
| 1 | 2 |
| 2 | 0 |
+-------------+---------------+
๐ก Note:
Employee 1 has 3 shifts where two overlaps occur: (09:00-13:00 overlaps with 12:00-16:00) and (12:00-16:00 overlaps with 15:00-19:00). Employee 2 has no overlapping shifts since there's a 2-hour gap between them.
example_2.py โ Multiple Employees
$
Input:
EmployeeShifts:
+-------------+------------+----------+
| employee_id | start_time | end_time |
+-------------+------------+----------+
| 1 | 08:00 | 12:00 |
| 2 | 10:00 | 14:00 |
| 2 | 13:00 | 17:00 |
| 3 | 09:00 | 11:00 |
+-------------+------------+----------+
โบ
Output:
+-------------+---------------+
| employee_id | overlap_count |
+-------------+---------------+
| 1 | 0 |
| 2 | 1 |
| 3 | 0 |
+-------------+---------------+
๐ก Note:
Employee 1 has only one shift (no overlaps). Employee 2's shifts (10:00-14:00) and (13:00-17:00) overlap since 14:00 > 13:00. Employee 3 has only one shift.
example_3.py โ Edge Case: Same Start Times
$
Input:
EmployeeShifts:
+-------------+------------+----------+
| employee_id | start_time | end_time |
+-------------+------------+----------+
| 1 | 09:00 | 12:00 |
| 1 | 09:00 | 15:00 |
| 1 | 10:00 | 14:00 |
+-------------+------------+----------+
โบ
Output:
+-------------+---------------+
| employee_id | overlap_count |
+-------------+---------------+
| 1 | 3 |
+-------------+---------------+
๐ก Note:
All three shift pairs overlap: (09:00-12:00 & 09:00-15:00), (09:00-12:00 & 10:00-14:00), and (09:00-15:00 & 10:00-14:00). Total overlapping pairs = 3.
Constraints
- 1 โค number of shifts โค 104
- 1 โค employee_id โค 103
- start_time and end_time are in HH:MM format
- start_time < end_time for all shifts
- (employee_id, start_time) is unique
Visualization
Tap to expand
Understanding the Visualization
1
Timeline Setup
Place all shifts for each employee on a time axis
2
Overlap Detection
Identify where shift bars intersect on the timeline
3
Count Pairs
Count each overlapping pair once for the final result
4
Aggregate Results
Sum overlap counts per employee and sort by ID
Key Takeaway
๐ฏ Key Insight: Visualizing shifts on a timeline makes it clear that overlaps occur when one shift's end time extends past another shift's start time, which translates directly to the SQL condition `s1.end_time > s2.start_time`.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code