Hone logo
Hone
Problems

Calculate Total Employee Work Time

This challenge involves processing a list of employee time entries to determine the total duration each employee has spent working. This is a common task in payroll, project management, and resource allocation, where understanding individual contributions and total work hours is crucial.

Problem Description

You are given a list of time entries, where each entry represents a period an employee was logged in or working. Each entry contains the employee's identifier, a start timestamp, and an end timestamp. Your task is to calculate the total time spent by each unique employee across all their recorded entries.

Requirements:

  • Process a collection of time entries.
  • For each employee, sum up the durations of all their time entries.
  • The output should be a collection where each unique employee is mapped to their total accumulated work time.
  • Durations should be calculated in a consistent unit (e.g., minutes, seconds, or hours, as specified by the timestamps).

Expected Behavior:

  • If an employee has multiple entries, their durations should be summed.
  • If an employee has no entries, they should not appear in the output.
  • Timestamps are assumed to be valid and represent specific points in time.
  • The order of employees in the output does not matter.

Edge Cases:

  • Overlapping entries: Assume entries for the same employee might overlap. The problem asks for the sum of durations of each recorded entry, not the total distinct time periods an employee was working. So, if employee A has an entry from 1:00 to 2:00 and another from 1:30 to 2:30, the total duration is (2:00 - 1:00) + (2:30 - 1:30) = 1 hour + 1 hour = 2 hours.
  • Zero-duration entries: An entry where start time equals end time should contribute zero duration.
  • Empty input list: If the input list of time entries is empty, the output should also be empty.

Examples

Example 1:

Input:
[
  {"employee_id": "emp1", "start_time": 1678886400, "end_time": 1678890000}, // 1 hour
  {"employee_id": "emp2", "start_time": 1678886400, "end_time": 1678893600}, // 2 hours
  {"employee_id": "emp1", "start_time": 1678893600, "end_time": 1678897200}  // 1 hour
]
(Timestamps are in Unix epoch seconds)

Output:
{
  "emp1": 7200, // 2 hours in seconds
  "emp2": 7200  // 2 hours in seconds
}
Explanation:
emp1: (1678890000 - 1678886400) + (1678897200 - 1678893600) = 3600 + 3600 = 7200 seconds.
emp2: (1678893600 - 1678886400) = 7200 seconds.

Example 2:

Input:
[
  {"employee_id": "A", "start_time": 1000, "end_time": 1500},
  {"employee_id": "B", "start_time": 1200, "end_time": 1800},
  {"employee_id": "A", "start_time": 2000, "end_time": 2200},
  {"employee_id": "C", "start_time": 2500, "end_time": 2500}
]
(Timestamps are generic numerical units)

Output:
{
  "A": 700, // 500 + 200
  "B": 600  // 600
}
Explanation:
Employee A worked for 1500 - 1000 = 500 units, and then 2200 - 2000 = 200 units. Total: 700 units.
Employee B worked for 1800 - 1200 = 600 units.
Employee C had a zero-duration entry.

Example 3: (Overlapping Entries)

Input:
[
  {"employee_id": "X", "start_time": 500, "end_time": 1000}, // Duration 500
  {"employee_id": "Y", "start_time": 600, "end_time": 900},  // Duration 300
  {"employee_id": "X", "start_time": 800, "end_time": 1200}  // Duration 400
]

Output:
{
  "X": 900, // 500 + 400
  "Y": 300  // 300
}
Explanation:
Employee X's first entry is 500 units long. Their second entry is 400 units long. Total: 500 + 400 = 900 units.
Employee Y's entry is 300 units long.

Constraints

  • The number of time entries will be between 0 and 100,000.
  • Employee IDs will be strings and will not be empty.
  • Timestamps will be non-negative integers.
  • For each entry, start_time will be less than or equal to end_time.
  • The total accumulated time for any single employee will not exceed 2^63 - 1 (to avoid overflow in standard integer types).
  • The solution should be efficient enough to process 100,000 entries within a few seconds.

Notes

  • Consider using a data structure that allows for efficient lookups and updates of employee totals. A hash map (or dictionary) is a good candidate.
  • Ensure you handle the calculation of duration correctly (end time minus start time).
  • The exact unit of time (seconds, minutes, etc.) depends on the input timestamps. The goal is to calculate the total duration in the same unit as the input.
  • Think about how to process the entries to aggregate the time for each employee.
Loading editor...
plaintext