Coding Challenge Practice – Question 52

The task is to implement findMeetingSlots(schedules) that finds available meeting slots for all members to have a meeting, given their busy schedules.

The boilerplate code

function findMeetingSlots(schedules) {
// your code here
}

Flatten a…


This content originally appeared on DEV Community and was authored by Bukunmi Odugbesan

The task is to implement findMeetingSlots(schedules) that finds available meeting slots for all members to have a meeting, given their busy schedules.

The boilerplate code

function findMeetingSlots(schedules) {
  // your code here
}

Flatten all members' schedules into one array

let allBusy = schedules.flat();

Sort the schedules by start time.

allBusy.sort((a, b) => a[0] - b[0]);

Merge overlapping time intervals

 let merged = [];
  for (let interval of allBusy) {
    if (!merged.length || merged[merged.length - 1][1] < interval[0]) {
      merged.push(interval);
    } else {
      merged[merged.length - 1][1] = Math.max(merged[merged.length - 1][1], interval[1]);
    }
  }

Find the gaps between the merged busy intervals - these are the available meeting slots

let available = [];
  let start = 0;
  for (let [s, e] of merged) {
    if (start < s) available.push([start, s]);
    start = e;
  }

Add the final available slot until 24

if (start < 24) available.push([start, 24]);

The final code:

function findMeetingSlots(schedules) {
  // your code here
  let allBusy = schedules.flat();

  allBusy.sort((a,b) => a[0] - b[0]);

  let merged = [];
  for(let interval of allBusy) {
    if(!merged.length || merged[merged.length - 1][1] < interval[0]) {
      merged.push(interval)
    } else {
      merged[merged.length - 1][1] = Math.max(merged[merged.length - 1][1], interval[1])
    }
  }

  let available = [];
  let start = 0;
  for(let[s,e] of merged) {
    if(start < s) available.push([start,s]);
    start = e;
  }
  if (start < 24) available.push([start, 24]);

  return available;
}

That's all folks!


This content originally appeared on DEV Community and was authored by Bukunmi Odugbesan


Print Share Comment Cite Upload Translate Updates
APA

Bukunmi Odugbesan | Sciencx (2025-11-11T22:34:43+00:00) Coding Challenge Practice – Question 52. Retrieved from https://www.scien.cx/2025/11/11/coding-challenge-practice-question-52/

MLA
" » Coding Challenge Practice – Question 52." Bukunmi Odugbesan | Sciencx - Tuesday November 11, 2025, https://www.scien.cx/2025/11/11/coding-challenge-practice-question-52/
HARVARD
Bukunmi Odugbesan | Sciencx Tuesday November 11, 2025 » Coding Challenge Practice – Question 52., viewed ,<https://www.scien.cx/2025/11/11/coding-challenge-practice-question-52/>
VANCOUVER
Bukunmi Odugbesan | Sciencx - » Coding Challenge Practice – Question 52. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/11/11/coding-challenge-practice-question-52/
CHICAGO
" » Coding Challenge Practice – Question 52." Bukunmi Odugbesan | Sciencx - Accessed . https://www.scien.cx/2025/11/11/coding-challenge-practice-question-52/
IEEE
" » Coding Challenge Practice – Question 52." Bukunmi Odugbesan | Sciencx [Online]. Available: https://www.scien.cx/2025/11/11/coding-challenge-practice-question-52/. [Accessed: ]
rf:citation
» Coding Challenge Practice – Question 52 | Bukunmi Odugbesan | Sciencx | https://www.scien.cx/2025/11/11/coding-challenge-practice-question-52/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.