Find Days Available to All Participants
Arrays & HashingAuto-graded · 18 test casesLast updated
Tests set intersection logic and handling of multiple collections to find common elements across all inputs.
You are building a scheduling tool that helps teams find meeting times. Each team member provides a list of day numbers when they are free to meet.
Write a function that takes a list of availability schedules (one per person) and returns the day numbers when all participants are available, sorted in ascending order.
Each schedule is represented as a list of integers, where each integer is a day number. Day numbers can appear in any order within a schedule and may contain duplicates.
If no common days exist across all schedules, return an empty list. If the input is empty (no participants), return an empty list.
Constraints
- 0 ≤ number of participants ≤ 100
- 0 ≤ days per participant ≤ 1000
- Day numbers are non-negative integers
- Days within one participant's schedule may be unsorted or contain duplicates
- The output must be sorted in ascending order with no duplicates
Examples
Example 1
Input: common_available_days([[1, 2, 3], [2, 3, 4], [0, 2, 3]])
Output: [2, 3]
Example 2
Input: common_available_days([[1, 5, 7], [5, 7], [5, 6, 7]])
Output: [5, 7]
Hints
Hint 1
Consider using set operations to efficiently find elements that appear in all collections.
Hint 2
Duplicates within one person's schedule should be treated as a single availability.
Hint 3
Starting with the shortest schedule can reduce unnecessary comparisons.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core task is to find the intersection of multiple lists — the days that appear in every participant's schedule.
The most direct approach uses set intersection:
- Handle the empty input case by returning an empty list immediately.
- Convert one schedule to a set to establish our initial candidate days. Starting with the shortest schedule minimizes the initial set size.
- Iteratively intersect this set with each remaining participant's schedule using
intersection_update. This removes any day that the current participant cannot attend. - If at any point the common set becomes empty, we can short-circuit and return immediately since no days work for everyone.
- Convert the final set to a sorted list for the output.
Why this works: A day appears in the output if and only if it appears in every input schedule. Set intersection captures this exactly — a day remains in the common set only if it survives intersection with all schedules.
Duplicate days within one person's schedule are naturally handled because converting to a set deduplicates automatically.
Complexity
Let N be the number of participants and M be the average number of days per participant.
- Time: O(N × M) to iterate through all schedules and perform set operations. The final sort is O(K log K) where K is the size of the intersection, but K ≤ M, so this is dominated by the intersection work.
- Space: O(M) for the set storing common days, plus O(M) for intermediate sets created during intersection operations.
If all participants have disjoint schedules, the early termination after the first intersection saves work.
Solution
from typing import List
def common_available_days(calendars: List[List[int]]) -> List[int]:
if not calendars:
return []
smallest = min(calendars, key=len)
common = set(smallest)
for available_days in calendars:
common.intersection_update(available_days)
if not common:
return []
return sorted(common)