Google Interview: How to calculate if given job can be done within a maximum CPU

I have recently received this interview question and got stuck: How to calculate if given a maximum CPU, will the job be true or false over time??

Let's say if we have a Job class with a constructor with startTime, duration, and cups for this specific job. We need to create a function with a given maxCpus, and see if the jobs will either be possible for the CPU requirement or not, return true or false.

Below is some example of the true and false cases.

Example: maxCpus = 3
0123456789
0---(3) 0--------(2)
  0------------(1) => false

Example: maxCpus = 3
0---3 0--------2 0-------1 => true

Example:
0 1 2 3 4 5 6 7 8 9
    0 - - - (start: 2, duration: 4)
      0 - - (start: 3, duration: 3)

earliest start ---------------- latest end time
jobs = [{startTime: 1, duration: 2, cpus: 5}, {}];
       [{}, {}, {}]

Below is some of the code I wrote for this question, but be aware that my solution is not correct!!!

function checkSchedule(maxCpus, jobs){
 let check;
 for (job in jobs) {
   const earliestStart = Math.min(...Object.values(job.startTime))
   const latestEnd = Math.max(job.startTime + (job.duration - 1))
   let endPoint = job.startTime + (job.duration - 1);
 }
 if(maxCpus <= ){
   check = true
 } else {
   check = false
 }
 return check;
}

I am still not able to figure out the solution to this question.

Please help. Thank you very much!

Comments (8)