Challenge - 5 Problems
Job Scheduling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Job Scheduling with Deadlines Algorithm
What is the output of the following job scheduling code that schedules jobs to maximize profit within their deadlines?
DSA Javascript
const jobs = [
{id: 'a', deadline: 2, profit: 100},
{id: 'b', deadline: 1, profit: 19},
{id: 'c', deadline: 2, profit: 27},
{id: 'd', deadline: 1, profit: 25},
{id: 'e', deadline: 3, profit: 15}
];
function jobScheduling(jobs) {
jobs.sort((a, b) => b.profit - a.profit);
const result = Array(jobs.length).fill(null);
for (const job of jobs) {
for (let j = Math.min(jobs.length, job.deadline) - 1; j >= 0; j--) {
if (result[j] === null) {
result[j] = job.id;
break;
}
}
}
return result.filter(x => x !== null).join(' -> ') + ' -> null';
}
console.log(jobScheduling(jobs));Attempts:
2 left
💡 Hint
Sort jobs by profit descending, then assign each job to the latest available slot before its deadline.
✗ Incorrect
The algorithm sorts jobs by profit descending: a(100), c(27), d(25), b(19), e(15). It tries to schedule each job in the latest free slot before its deadline. 'a' goes to slot 1, 'c' to slot 0 (deadline 2 means slot 1 or 0), 'e' to slot 2. 'd' and 'b' cannot be scheduled because slots are taken. So output is 'c -> a -> e -> null'.
🧠 Conceptual
intermediateMaximum Number of Jobs Scheduled
Given 4 jobs with deadlines and profits, what is the maximum number of jobs that can be scheduled without missing deadlines?
Attempts:
2 left
💡 Hint
Consider the maximum deadline and available slots.
✗ Incorrect
The maximum number of jobs scheduled equals the number of available time slots before the maximum deadline. If maximum deadline is 3, at most 3 jobs can be scheduled.
🔧 Debug
advancedIdentify the Bug in Job Scheduling Code
What error does the following job scheduling code produce when run?
DSA Javascript
function scheduleJobs(jobs) {
jobs.sort((a, b) => a.profit - b.profit);
const result = Array(jobs.length).fill(null);
for (const job of jobs) {
for (let j = job.deadline - 1; j >= 0; j--) {
if (result[j] === null) {
result[j] = job.id;
break;
}
}
}
return result.filter(x => x !== null).join(' -> ') + ' -> null';
}
const jobs = [
{id: 'x', deadline: 2, profit: 50},
{id: 'y', deadline: 1, profit: 60}
];
console.log(scheduleJobs(jobs));Attempts:
2 left
💡 Hint
Check the sorting order and how jobs are assigned to slots.
✗ Incorrect
The code sorts jobs by ascending profit, so job 'x' (50) is scheduled before 'y' (60). 'x' is assigned to slot 1 (deadline 2), 'y' tries slot 0 (deadline 1) and slot 0 is free, so 'y' is assigned there. The output is 'y -> x -> null'. The bug is the ascending sort (should be descending to prioritize higher profit).
📝 Syntax
advancedSyntax Error in Job Scheduling Code
Which option contains the syntax error when trying to implement job scheduling with deadlines?
Attempts:
2 left
💡 Hint
Check array indexing and loop boundaries carefully.
✗ Incorrect
Option D uses 'for (let j = job.deadline; j >= 0; j--)' which tries to access result[j] where j can be equal to job.deadline, which is out of array bounds (0-based index). The correct loop should start from job.deadline - 1.
🚀 Application
expertMaximum Profit from Job Scheduling
Given the jobs below, what is the maximum total profit achievable by scheduling jobs within their deadlines?
DSA Javascript
const jobs = [
{id: 'j1', deadline: 4, profit: 70},
{id: 'j2', deadline: 1, profit: 80},
{id: 'j3', deadline: 1, profit: 30},
{id: 'j4', deadline: 2, profit: 100},
{id: 'j5', deadline: 3, profit: 40},
{id: 'j6', deadline: 2, profit: 20}
];Attempts:
2 left
💡 Hint
Schedule jobs by descending profit and assign to latest free slot before deadline.
✗ Incorrect
Sorted by profit descending: j4(100), j2(80), j1(70), j5(40), j3(30), j6(20).
Schedule j4 at slot 1 (deadline 2), j2 at slot 0 (deadline 1), j1 at slot 3 (deadline 4), j5 at slot 2 (deadline 3).
j3 and j6 cannot be scheduled because slots are full.
Total profit = 100 + 80 + 70 + 40 = 290.
But we must check if j1 can be scheduled at slot 3 (index 3), which is valid.
So total profit is 290.
Option B (310) is incorrect, correct is 290 (Option B).
