Find Overlapping Shifts II - Problem

You are given a table EmployeeShifts that contains information about employee work shifts with start and end times.

Write a solution to analyze overlapping shifts for each employee. Two shifts are considered overlapping if they occur on the same date and one shift's end_time is later than another shift's start_time.

For each employee, calculate:

  • The maximum number of shifts that overlap at any given time
  • The total duration of all overlaps in minutes

Return the result table ordered by employee_id in ascending order.

Table Schema

EmployeeShifts
Column Name Type Description
employee_id PK int Employee identifier
start_time PK datetime Shift start timestamp
end_time datetime Shift end timestamp
Primary Key: (employee_id, start_time)
Note: Each row represents one work shift for an employee

Input & Output

Example 1 — Multiple Overlapping Shifts
Input Table:
employee_id start_time end_time
1 2023-10-01 09:00:00 2023-10-01 17:00:00
1 2023-10-01 15:00:00 2023-10-01 23:00:00
1 2023-10-01 16:00:00 2023-10-02 00:00:00
2 2023-10-01 09:00:00 2023-10-01 17:00:00
2 2023-10-01 11:00:00 2023-10-01 19:00:00
3 2023-10-01 09:00:00 2023-10-01 17:00:00
Output:
employee_id max_overlapping_shifts total_overlap_duration
1 3 600
2 2 360
3 1 0
💡 Note:

Employee 1 has 3 overlapping shifts with maximum 3 simultaneous shifts from 16:00-17:00. Total overlaps: 2hrs (15:00-17:00) + 1hr (16:00-17:00) + 7hrs (16:00-23:00) = 600 minutes. Employee 2 has 2 shifts overlapping 6 hours (11:00-17:00) = 360 minutes. Employee 3 has only 1 shift, so no overlaps.

Example 2 — No Overlaps
Input Table:
employee_id start_time end_time
1 2023-10-01 09:00:00 2023-10-01 12:00:00
1 2023-10-01 13:00:00 2023-10-01 17:00:00
2 2023-10-01 08:00:00 2023-10-01 16:00:00
Output:
employee_id max_overlapping_shifts total_overlap_duration
1 1 0
2 1 0
💡 Note:

Employee 1 has two consecutive shifts with no overlap (12:00-13:00 gap). Employee 2 has only one shift. Both have max_overlapping_shifts of 1 and total_overlap_duration of 0.

Constraints

  • 1 ≤ employee_id ≤ 1000
  • start_time < end_time for all shifts
  • All timestamps are valid datetime values

Visualization

Tap to expand
Find Overlapping Shifts II INPUT emp_id date start end 101 Jan-15 08:00 12:00 101 Jan-15 10:00 14:00 101 Jan-15 11:00 13:00 102 Jan-15 09:00 11:00 102 Jan-15 10:30 12:30 Timeline View (Emp 101) 08:00 10:00 12:00 14:00 Shift 1 Shift 2 Shift 3 Overlap Zone Shifts grouped by employee + date ALGORITHM STEPS 1 Group Shifts Group by employee_id + date 2 Create Events Mark start (+1) and end (-1) Events: [(08:00,+1), (10:00,+1), (11:00,+1), (12:00,-1), (13:00,-1), (14:00,-1)] 3 Line Sweep Sort events, track active count 08:00: count=1 (max=1) 10:00: count=2 (max=2) 11:00: count=3 (max=3) [PEAK] 12:00: count=2 ... 4 Calculate Overlap Sum overlap when count > 1 Total = sum of (count-1) * duration FINAL RESULT emp_id max_sim total 101 3 180 102 2 30 Employee 101: 3 shifts overlap at 11:00-12:00 Total overlap: 180 minutes (10:00-12:00 + 11:00-13:00) Employee 102: 2 shifts overlap at 10:30-11:00 Total overlap: 30 minutes OK Grouped by employee_id Key Insight: The Line Sweep algorithm efficiently finds overlapping intervals by converting shift boundaries to events. Start times add +1, end times add -1. The maximum active count gives max simultaneous overlaps. Total overlap = sum of extra shift minutes when count exceeds 1. Time complexity: O(n log n) per employee. TutorialsPoint - Find Overlapping Shifts II | Optimal Solution (Line Sweep Algorithm)
Asked in
Amazon 28 Microsoft 22 Google 18
27.7K Views
Medium Frequency
~25 min Avg. Time
892 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