Prepin
Log in

Create an algorithm to identify times when several individuals are free.

Approach / Explanation

This inquiry seeks an algorithm that can pinpoint overlapping intervals from a list of specified intervals, such as available times for several individuals. This data could be beneficial for organizing group meetings or determining shared availability. When addressing this issue, keep the following points in mind: This challenge is a variation of the "Merge Intervals" algorithm problem. You will need to figure out how to gather the schedules of multiple individuals and convert them into a workable format. Make sure to account for edge cases, like situations where there is no overlapping interval among all individuals.

Suggested Answer

A simple approach to solve this issue is to combine all the intervals and then identify any gaps. This method is effective even with an increasing number of individuals, and the times do not need to be sorted. Here’s a potential implementation in Python: def find_common_availability ( schedules ) : merged = [ ] for schedule in schedules : for times in schedule : if not merged or merged [ - 1 ] [ 1 ] < times [ 0 ] : merged . append ( times ) else : merged [ - 1 ] [ 1 ] = max ( merged [ - 1 ] [ 1 ], times [ 1 ) intervals = [ ] for i in range ( len ( merged ) - 1 ) : intervals . append ( ( merged [ i ] [ 1 ], merged [ i + 1 ] [ 0 ] ) ) return intervals In this algorithm, we first combine all the intervals. Once merged, these intervals indicate the times when individuals are occupied. The spaces between these intervals represent the moments when everyone is available, which are then gathered and returned as the output.

Alternative Answer

Additionally, we can incorporate a priority queue within the algorithm to manage the intervals more effectively. For simplicity, we assume that both the start and end times of the intervals are represented as integers. Below is a potential implementation in Python: import heapq def available_times ( schedules ) : times = [ ] for i , schedule in enumerate ( schedules ) : for time in schedule : start , end = time times . append ( ( start , i , False ) ) times . append ( ( end , i , True ) ) heapq . heapify ( times ) overlaps = [ 0 ] * len ( schedules ) intervals = [ ] while times : time , user , is_end = heapq . heappop ( times ) overlaps [ user ] = 0 if is_end else 1 if sum ( overlaps ) == 0 and times : intervals . append ( ( time , times [ 0 ] [ 0 ] ) ) return intervals In this code, we utilize the heapq module to create a priority queue for all time intervals, while the overlaps list tracks the overlaps among users. During each iteration, if the total number of overlaps is zero, it indicates that everyone is free during that time slot from the current time to the start of the next time slot, prompting us to add that time slot to our list of available times.

Question Details

Difficulty & Category

Medium
technical

Companies

Google

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.