Code Logo

Find First Free Time Slot

Published at19 Apr 2026
Searching & Sorting Medium 4 views
Like0

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.

Learn about our pseudocode specification
Guide

Example Input & Output

Example 1
Input
busy = [[9,10],[12,13],[14,16]], need = 2, day_start = 8, day_end = 18
Output
10
Explanation

The first gap long enough is from 10 to 12, so the meeting can start at 10.

Example 2
Input
busy = [[9,12],[12,14]], need = 1, day_start = 9, day_end = 15
Output
14
Explanation

The first two blocks connect into one busy stretch until 14, so the earliest free slot starts there.

Example 3
Input
busy = [[8,12],[13,17]], need = 2, day_start = 8, day_end = 17
Output
-1
Explanation

There is only a one-hour gap between the blocks, which is not enough.

Algorithm Flow

Recommendation Algorithm Flow for Find First Free Time Slot
Recommendation Algorithm Flow for Find First Free Time Slot

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:

next_start - current_end >= need

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

Pseudocode - Approach 1
program find_first_free_slot
dictionary
   need, day_start, day_end, n, i, j, answer, current_end: integer
   busy: array[1..100] of integer
   temp: integer
algorithm
   input(busy, need, day_start, day_end)
   n <- busy.length
   answer <- -1
   if n = 0 then
      if day_end - day_start >= need then answer <- day_start endif
   else
      for i <- 0 to n - 2 do
         for j <- 0 to n - i - 2 do
            if busy[j][0] > busy[j + 1][0] then
               temp <- busy[j]
               busy[j] <- busy[j + 1]
               busy[j + 1] <- temp
            endif
         endfor
      endfor
      current_end <- day_start
      for i <- 0 to n - 1 do
         if busy[i][0] > current_end then
            if answer = -1 AND busy[i][0] - current_end >= need then answer <- current_end endif
         endif
         if busy[i][1] > current_end then current_end <- busy[i][1] endif
      endfor
      if answer = -1 AND day_end - current_end >= need then answer <- current_end endif
   endif
   output(answer)
endprogram