Days when the whole team is free
A team is planning an offsite over a window of num_days days, numbered from 1 to num_days. Each team member has entered busy blocks as triples (person_id, start_day, end_day). Both ends are inclusive. A person may have many blocks, and their blocks may overlap or touch.
Implement:
def find_common_free_days(num_days: int, blocks: list[tuple[int, int, int]]) -> list[int]:
...
Return, in ascending order, every day in 1..num_days on which no one is busy.
Also implement merge_per_person(blocks), which returns each person's busy days as sorted, non-overlapping inclusive ranges (blocks that overlap or are adjacent are joined).
Constraints
-
1 <= num_days <= 10^9,0 <= len(blocks) <= 10^5. - A block may start before day 1 or end after
num_days; only the part inside the window counts. Ignore blocks withstart_day > end_day. - Your running time should not depend on how long the blocks are.
Example 1
num_days = 12
blocks = [(7, 3, 6), (9, 5, 8), (7, 11, 11)]
Person 7 is busy on days 3-6 and 11; person 9 on days 5-8. Together they cover 3-8 and 11.
Output: [1, 2, 9, 10, 12]
Example 2
num_days = 6
blocks = [(1, 1, 2), (2, 3, 6)]
The blocks touch (day 2, then day 3), so there is no gap. Output: []
Follow-ups you may be asked: return free ranges instead of individual days; find the longest common free stretch; return days on which at least k people are free; support adding and removing blocks between queries.
Reconstructed from the reported question; details may differ from what interviewers ask.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.

