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
+------------------+---------+
| 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
Employee Shift Overlap VisualizationEmployee 1 Timeline:08:0010:0012:0014:0016:0018:00Shift AShift BShift COverlap 1Overlap 2Employee 2 Timeline:Shift DShift ENo OverlapResults Summary:Employee 1: 2 overlapping pairs โ†’ overlap_count = 2Employee 2: 0 overlapping pairs โ†’ overlap_count = 0SQL Logic: WHERE s1.end_time > s2.start_time๐ŸŽฏ Key: Each intersection of timeline bars represents one overlapping shift pair
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`.
Asked in
Amazon 45 Meta 32 Google 28 Microsoft 22 Apple 18
23.6K Views
Medium-High Frequency
~15 min Avg. Time
847 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