← Questions

Employee shift timeline table

0
GoogleMicrosoftCodingLast asked Jun 2026

Each shift is given as [name, start, end], with integer times and start <= end. A shift covers the half-open interval [start, end). One employee may have several shifts, and they may touch or overlap.

Implement shift_timeline(schedules) that returns the shift timeline as a list of rows (start, end, names):

  • Split time into the longest stretches in which the set of people on shift does not change.
  • Leave out stretches when nobody is working.
  • Rows are in time order and do not overlap. Two rows that touch must have different people, so back-to-back shifts of the same person form one row.
  • Inside a row, list names in the order each name first appears in the input, not by start time.

Constraints: up to 10^5 shifts; times fit in a 32-bit integer. Zero-length shifts cover no time.

Example 1

schedules = [["Kai", 9, 13], ["Mira", 11, 17], ["Kai", 15, 18], ["Lee", 20, 22]]

Output:

[(9, 11, ["Kai"]), (11, 13, ["Kai", "Mira"]), (13, 15, ["Mira"]),
 (15, 17, ["Kai", "Mira"]), (17, 18, ["Kai"]), (20, 22, ["Lee"])]

Nobody works from 18 to 20, so there is no row for it.

Example 2

schedules = [["Zed", 5, 8], ["Amy", 1, 6], ["Amy", 6, 7]]

Output: [(1, 5, ["Amy"]), (5, 7, ["Zed", "Amy"]), (7, 8, ["Zed"])]. Zed is listed first because his name appears first in the input, and Amy's two shifts join into one stretch at 6.

Be ready to print the result as a table with columns start, end, employees.

Reconstructed from the reported question; details may differ from what interviewers ask.

Write and test your solution herePython in your browser, the question's tests level by level, saved for you.Sign in to start coding