Plan region go-lives around soak times and change windows

Agent buildMedium25 min

Your task

A new version of your support agent goes live region by region. A region that depends on others waits until each of them has soaked: run the new version for a set number of hours while the on-call team is watching. The team only watches during a daily change window, and go-lives happen only inside it too.

Write go_live_hours(regions, start, window) that returns the hour each region goes live.

  • regions: a list of [name, needs, soak], in any order. needs lists the names of the regions it waits for. soak is how many watched hours this region must run the new version before a region that needs it may go live, from 0 to 100,000. Names are unique.
  • start: the first hour any region may go live, 0 or more. Hours count from midnight of day 0, so hour 26 is 02:00 on day 1.
  • window: [open, close], whole hours with 0 <= open < close <= 24. Hour h is inside the window when open <= h % 24 < close.
  • Return a dict that maps every region's name to its go-live hour, or to -1 if it can never go live.

Rules:

  1. A region goes live at the earliest whole hour that is at least start, inside the window, and no earlier than the end of the soak of every region it needs.
  2. A region that goes live at hour t with soak s ends its soak at the first hour e such that s of the hours t, t + 1, ..., e - 1 are inside the window. A soak of 0 ends at t.
  3. A region can never go live if it needs a name that is not in regions. The same goes for a region that needs itself through a chain of needs, or that needs a region that can never go live.
  4. Regions do not compete: any number of them may go live in the same hour.

Examples

go_live_hours(regions: list[list], start: int, window: list[int]) -> dict[str, int]
Example 1
Inputgo_live_hours([["us-east", [], 24], ["us-west", ["us-east"], 12], ["eu", ["us-east"], 6]], 9, [9, 17])
Output{"us-east": 9, "us-west": 81, "eu": 81}
Example 2
Inputgo_live_hours([["eu-2", ["eu"], 0], ["eu", ["us"], 4], ["us", [], 10]], 0, [8, 20])
Output{"eu-2": 34, "eu": 18, "us": 8}
Example 3
Inputgo_live_hours([["a", ["b"], 1], ["b", ["a"], 1], ["c", ["a"], 1], ["d", ["nowhere"], 1], ["e", [], 1]], 5, [0, 24])
Output{"a": -1, "b": -1, "c": -1, "d": -1, "e": 5}