Katkat

Find Live Hand-Off Windows Between Follow-the-Sun Support Teams

StripeSoftware Engineer · Online Assessment

Last updated

Find recurring weekly windows where at least two regional support teams are simultaneously online, after converting each team's local shift schedule to a shared UTC timeline and handling the week's wraparound, then filter by a lead time, a minimum overlap length and a results cap. Tests interval sweeping, time-zone arithmetic and turning a loosely specified brief into exact rules.

A support org staffs several regional teams, each covering its own recurring weekly shifts in its own local time. For a live hand-off between two teams to happen, both teams need to be online at the same moment. Build the planner that finds those overlap windows.

Time is tracked as an offset in minutes into a repeating week: W = 10080 (7 * 24 * 60), so minute 0 is the start of the week and minute 10079 is its last minute.

Part 1 — Overlap on a single UTC timeline

Each shift is a triple (team_id, start, end) already in UTC, with 0 <= start < end <= W. A team may have more than one shift. Find every maximal interval where two or more distinct teams have a shift covering that minute, and return those intervals as [start, end) pairs, sorted by start. Assume shifts belonging to the same team never overlap each other.

Part 2 — Local time and the week wraparound

Shifts are now given in each team's local time: (team_id, local_start, local_end, utc_offset), where utc_offset is local time minus UTC, in minutes (UTC+9 is 540, UTC-5 is -300). Converting to UTC is utc_time = local_time - utc_offset, but because the week repeats, a converted shift can land partly or wholly outside [0, W) and must be wrapped back into range — which can split one local shift into two UTC pieces straddling the week boundary. Recompute the overlap intervals from Part 1 on top of these converted, possibly-split shifts.

Part 3 — Lead time, minimum length, and a cap

def handoff_windows(now_utc: int, earliest_lag: int, min_overlap: int,
                     max_results: int, team_windows: list[tuple]) -> list[list[int]]:

From the overlap intervals computed in Part 2, keep only the ones that:

  • start at or after now_utc + earliest_lag (a hand-off can't be scheduled before the team has had notice);
  • last at least min_overlap minutes.

Return at most max_results of the surviving intervals, in chronological order by start.

Example

team_windows = [
    ("A", 480, 1200, 0),   # UTC team, online 08:00-20:00 Monday (minute 480-1200)
    ("B", 600, 1320, 0),   # UTC team, online 10:00-22:00 Monday
    ("C", 0, 480, 540),    # Tokyo (UTC+9) team, local 00:00-08:00 -> UTC 9540-10020
]

handoff_windows(0, 0, 60, 5, team_windows)
# -> [[600, 1200]]

Teams A and B overlap from minute 600 to 1200 (2 hours into A's shift through B's start... through to A's end); team C's converted window doesn't touch either.

Underspecified details to pin down

As with many real specs, several choices aren't fully determined by the prompt above; state and justify the ones you pick, for example: whether max_results selects the earliest windows or the longest ones, whether two teams whose online windows only touch at a single instant count as overlapping, and what happens if team_windows is empty.

Constraints

  • Up to 2000 shift entries across all teams.
  • 0 <= local_start < local_end <= W for every shift before conversion.
  • -720 <= utc_offset <= 840 (a generous real-world time-zone range).
  • 0 <= now_utc < W, 0 <= earliest_lag, 0 <= min_overlap <= W, 1 <= max_results.

Hints

Hint 1

Convert every local shift to UTC first, independently of the overlap logic — a shift that wraps past the week boundary becomes two pieces, each entirely inside [0, W).

Hint 2

Turn every converted piece into a pair of sweep events (a start that adds one to a running count, an end that subtracts one), sort all events by position, and walk through them once.

Hint 3

A 'two or more teams online' interval begins exactly when the running count crosses from below 2 to 2 or more, and ends when it drops back below 2 — track both transitions as you sweep.

Hint 4

Apply the lead-time, minimum-length and result-count filters only after you have the full list of overlap intervals; don't try to fold them into the sweep itself.

Follow-up questions

  • How would you change the sweep to require three or more teams online at once instead of two, and does anything about the transition logic need to change?
  • If a team's shift list could change while the planner is running, what's the cheapest way to recompute affected overlap windows rather than rebuilding everything?
  • How would you support a team whose shifts genuinely can overlap each other (for example, an overlapping on-call rotation), where you need distinct team count rather than raw event count?
  • What test cases would you write specifically to catch an off-by-one in the week-wraparound split?