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 and Profits
What is the output of the following Java code that schedules jobs to maximize profit given deadlines?
DSA Java
import java.util.*; class Job { int id, deadline, profit; Job(int id, int deadline, int profit) { this.id = id; this.deadline = deadline; this.profit = profit; } } public class Main { public static void main(String[] args) { Job[] jobs = { new Job(1, 4, 20), new Job(2, 1, 10), new Job(3, 1, 40), new Job(4, 1, 30) }; Arrays.sort(jobs, (a, b) -> b.profit - a.profit); int n = jobs.length; int maxDeadline = 0; for (Job job : jobs) { if (job.deadline > maxDeadline) maxDeadline = job.deadline; } int[] result = new int[maxDeadline + 1]; Arrays.fill(result, -1); int count = 0, profit = 0; for (Job job : jobs) { for (int j = job.deadline; j > 0; j--) { if (result[j] == -1) { result[j] = job.id; count++; profit += job.profit; break; } } } System.out.println("Jobs scheduled: " + count); System.out.println("Total profit: " + profit); } }
Attempts:
2 left
💡 Hint
Sort jobs by profit descending and assign each job to the latest available slot before its deadline.
✗ Incorrect
The jobs are sorted by profit: Job 3 (40), Job 4 (30), Job 1 (20), Job 2 (10). Job 3 is scheduled at slot 1, Job 4 cannot be scheduled at slot 1 (taken), so skipped, Job 1 is scheduled at slot 4, Job 2 cannot be scheduled (slot 1 taken). Total jobs scheduled = 2, total profit = 40 + 20 = 60 (option A).
❓ Predict Output
intermediateOutput of Job Scheduling with Overlapping Deadlines
What is the output of this Java code that schedules jobs with overlapping deadlines to maximize profit?
DSA Java
import java.util.*; class Job { int id, deadline, profit; Job(int id, int deadline, int profit) { this.id = id; this.deadline = deadline; this.profit = profit; } } public class Main { public static void main(String[] args) { Job[] jobs = { new Job(1, 2, 100), new Job(2, 1, 19), new Job(3, 2, 27), new Job(4, 1, 25), new Job(5, 3, 15) }; Arrays.sort(jobs, (a, b) -> b.profit - a.profit); int n = jobs.length; int maxDeadline = 0; for (Job job : jobs) { if (job.deadline > maxDeadline) maxDeadline = job.deadline; } int[] result = new int[maxDeadline + 1]; Arrays.fill(result, -1); int count = 0, profit = 0; for (Job job : jobs) { for (int j = job.deadline; j > 0; j--) { if (result[j] == -1) { result[j] = job.id; count++; profit += job.profit; break; } } } System.out.println("Jobs scheduled: " + count); System.out.println("Total profit: " + profit); } }
Attempts:
2 left
💡 Hint
Assign jobs starting from highest profit to latest available slot before deadline.
✗ Incorrect
Jobs sorted by profit: 1(100), 3(27), 4(25), 2(19), 5(15). Schedule Job 1 at slot 2, Job 3 at slot 1, Job 4 cannot be scheduled (slot 1 taken), Job 2 cannot be scheduled (slot 1 taken), Job 5 at slot 3. So 3 jobs scheduled with total profit 100+27+15=142.
🔧 Debug
advancedIdentify the bug in Job Scheduling code
The following Java code is intended to schedule jobs to maximize profit given deadlines. It compiles but produces incorrect output. What is the bug?
DSA Java
import java.util.*; class Job { int id, deadline, profit; Job(int id, int deadline, int profit) { this.id = id; this.deadline = deadline; this.profit = profit; } } public class Main { public static void main(String[] args) { Job[] jobs = { new Job(1, 2, 100), new Job(2, 1, 19), new Job(3, 2, 27), new Job(4, 1, 25), new Job(5, 3, 15) }; Arrays.sort(jobs, (a, b) -> a.profit - b.profit); int n = jobs.length; int maxDeadline = 0; for (Job job : jobs) { if (job.deadline > maxDeadline) maxDeadline = job.deadline; } int[] result = new int[maxDeadline + 1]; Arrays.fill(result, -1); int count = 0, profit = 0; for (Job job : jobs) { for (int j = job.deadline; j > 0; j--) { if (result[j] == -1) { result[j] = job.id; count++; profit += job.profit; break; } } } System.out.println("Jobs scheduled: " + count); System.out.println("Total profit: " + profit); } }
Attempts:
2 left
💡 Hint
Check the sorting order of jobs by profit.
✗ Incorrect
The code sorts jobs by ascending profit (a.profit - b.profit), so low profit jobs are scheduled first, which is incorrect. It should be descending order (b.profit - a.profit) to maximize profit.
🧠 Conceptual
advancedMaximum number of jobs scheduled with deadlines
Given a list of jobs with deadlines and profits, what is the maximum number of jobs that can be scheduled if each job takes exactly 1 unit of time and no two jobs can be scheduled at the same time?
Attempts:
2 left
💡 Hint
Think about the time slots available to schedule jobs.
✗ Incorrect
Since each job takes 1 unit time and no overlap allowed, the maximum jobs scheduled cannot exceed the maximum deadline (time slots available).
🚀 Application
expertFind the scheduled jobs and total profit
Given the following jobs with id, deadline, and profit, which jobs will be scheduled and what is the total profit after applying the job scheduling algorithm?
DSA Java
Job[] jobs = {
new Job(1, 2, 50),
new Job(2, 1, 10),
new Job(3, 2, 20),
new Job(4, 1, 30),
new Job(5, 3, 40),
new Job(6, 2, 60)
};Attempts:
2 left
💡 Hint
Sort jobs by profit descending and assign each to latest free slot before deadline.
✗ Incorrect
Jobs sorted by profit: 6(60),1(50),5(40),4(30),3(20),2(10).
Schedule Job 6 at slot 2,
Job 1 at slot 1 (slot 2 taken by 6),
Job 5 at slot 3,
Jobs 4,3,2 cannot be scheduled due to slots taken.
Total profit = 60+50+40=150.
