Find First Free Time Slot
This problem comes from a very normal scheduling situation. You already have a list of busy time blocks in your day, and now you want to figure out when a new meeting can fit.
The goal is not to list every free gap. You only need the earliest start time where a meeting with a given duration can be placed between day_start and day_end.
For example, if your day runs from 8 to 18, your busy blocks are [[9,10],[12,13],[14,16]], and the new meeting needs 2 hours, the answer is 10. The block from 10 to 12 is long enough, so that is the first slot that works.
So the task is to look through the schedule, account for overlaps, and return the earliest valid start time. If no gap is big enough, return -1.
Example Input & Output
The first gap long enough is from 10 to 12, so the meeting can start at 10.
The first two blocks connect into one busy stretch until 14, so the earliest free slot starts there.
There is only a one-hour gap between the blocks, which is not enough.
Algorithm Flow

Solution Approach
A good way to solve this problem is to process the busy blocks in time order. That way, you can move from left to right through the day and check the gaps between them.
The first important step is to sort the blocks by their start time. Without that, you cannot reliably tell where the free spaces are.
Once the blocks are sorted, you keep track of the end of the current busy section. Then, each time you look at the next block, you ask whether the gap between them is large enough for the meeting.
The condition you care about is:
If that is true, then the meeting can start at current_end, and that is the earliest valid answer at that point.
You also need to be careful with overlapping blocks. If one block starts before the previous one ends, they really belong to one longer busy section. In that case, you should extend the current end instead of treating them as separate gaps.
After checking all the blocks, there is one last thing to do. You still need to test the gap from the end of the last busy block to day_end. If that space is big enough, then the answer starts there. If not, return -1.
So the full idea is: sort the schedule, merge overlaps as you go, and stop at the first gap that is long enough.
Best Answers
Solutions Coming Soon
Verified best solutions for this Challenge are still being analyzed and will be available soon.
Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
