Employee Task Duration and Concurrent Tasks - Problem

You're managing a company's task tracking system and need to analyze employee productivity. Given a database table of employee tasks with start and end times, your goal is to calculate two key metrics for each employee:

  • Total Task Hours: The sum of all task durations (excluding overlapping time) rounded down to the nearest full hour
  • Maximum Concurrent Tasks: The highest number of tasks an employee was working on simultaneously at any point

The challenge lies in properly handling overlapping tasks. When an employee works on multiple tasks simultaneously, you should only count the overlapped time once in the total duration calculation.

Example: If Task A runs from 9:00-11:00 (2 hours) and Task B runs from 10:00-12:00 (2 hours), the total working time is 3 hours (not 4), because the hour from 10:00-11:00 overlaps.

Return results ordered by employee_id in ascending order.

Input & Output

basic_example.sql โ€” Basic overlapping tasks
$ Input: Tasks: [(1,1001,'2023-05-01 08:00:00','2023-05-01 09:00:00'), (2,1001,'2023-05-01 08:30:00','2023-05-01 10:30:00')]
โ€บ Output: [(1001, 2, 2)]
๐Ÿ’ก Note: Employee 1001 has two overlapping tasks. Task 1 (60 min) + Task 2 (120 min) - overlap (30 min) = 150 min = 2.5 hours โ†’ 2 hours. Maximum concurrent tasks is 2 during the overlap period.
multiple_employees.sql โ€” Multiple employees with different patterns
$ Input: Tasks: [(1,1001,'08:00','09:00'), (2,1001,'08:30','10:30'), (3,1002,'09:00','10:00'), (4,1002,'11:00','12:00')]
โ€บ Output: [(1001, 2, 2), (1002, 2, 1)]
๐Ÿ’ก Note: Employee 1001: overlapping tasks total 2.5hโ†’2h, max concurrent 2. Employee 1002: non-overlapping tasks total 2h, max concurrent 1.
edge_case.sql โ€” Single task and complex overlaps
$ Input: Tasks: [(1,1001,'08:00','10:00'), (2,1001,'09:00','11:00'), (3,1001,'10:30','12:00'), (4,1002,'14:00','15:00')]
โ€บ Output: [(1001, 4, 2), (1002, 1, 1)]
๐Ÿ’ก Note: Employee 1001: Three tasks with partial overlaps create total coverage from 08:00-12:00 (4 hours). Max concurrent is 2. Employee 1002: Single 1-hour task.

Constraints

  • 1 โ‰ค number of tasks โ‰ค 104
  • 1 โ‰ค employee_id โ‰ค 109
  • start_time < end_time for all tasks
  • All datetime values are valid
  • Task duration is at least 1 minute

Visualization

Tap to expand
Employee Task Timeline AnalysisEmployee 1001Task 1Task 2Task 3Employee 1002Task 4Task 58:008:309:0010:3011:0012:00OverlapMax: 2 concurrentEvent Processing Timeline+1+1-1-1+1-1Active: 1Active: 2Active: 1Active: 1Total Active Time = Sum of periods with Active โ‰ฅ 1Employee 1001: 6 hours total, 2 max concurrent
Understanding the Visualization
1
Timeline Events
Mark when each task starts and ends, like noting when dishes go on/off the stove
2
Chronological Processing
Walk through time, updating how many tasks are active at each moment
3
Active Time Tracking
Sum up periods when at least one task was running
4
Peak Activity
Record the maximum number of simultaneous tasks
Key Takeaway
๐ŸŽฏ Key Insight: Transform the overlap problem into a chronological event stream. This allows us to efficiently track both total active time and peak concurrency in a single pass through sorted events.
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
48.0K Views
High Frequency
~25 min Avg. Time
1.9K 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