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 Maximum Profit
What is the output of the following C++ code that schedules jobs to maximize profit given deadlines?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Job { char id; int deadline; int profit; }; bool cmp(Job a, Job b) { return a.profit > b.profit; } int main() { vector<Job> jobs = {{'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27}, {'d', 1, 25}, {'e', 3, 15}}; sort(jobs.begin(), jobs.end(), cmp); int n = jobs.size(); vector<int> result(n, -1); vector<bool> slot(n, false); for (int i = 0; i < n; i++) { for (int j = min(n, jobs[i].deadline) - 1; j >= 0; j--) { if (!slot[j]) { slot[j] = true; result[j] = i; break; } } } for (int i = 0; i < n; i++) { if (result[i] != -1) { cout << jobs[result[i]].id << " "; } } return 0; }
Attempts:
2 left
💡 Hint
Sort jobs by profit descending and assign each job to the latest available slot before its deadline.
✗ Incorrect
Jobs are sorted by profit: a(100), c(27), d(25), b(19), e(15). Assign 'a' to slot 1 (deadline 2), 'c' to slot 0 (deadline 2), and 'e' to slot 2 (deadline 3). Output is 'a c e'.
🧠 Conceptual
intermediateMaximum Number of Jobs Scheduled
Given 5 jobs with deadlines and profits, what is the maximum number of jobs that can be scheduled without overlapping deadlines?
Attempts:
2 left
💡 Hint
Each job takes 1 unit time and must be completed before its deadline.
✗ Incorrect
The maximum number of jobs scheduled equals the number of available slots before deadlines. For the given jobs, 3 jobs can be scheduled without conflict.
🔧 Debug
advancedIdentify the Bug in Job Scheduling Code
What error will the following code produce when run?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Job { char id; int deadline; int profit; }; bool cmp(Job a, Job b) { return a.profit < b.profit; } int main() { vector<Job> jobs = {{'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27}}; sort(jobs.begin(), jobs.end(), cmp); int n = jobs.size(); vector<int> result(n, -1); vector<bool> slot(n, false); for (int i = 0; i < n; i++) { for (int j = min(n, jobs[i].deadline) - 1; j >= 0; j--) { if (!slot[j]) { slot[j] = true; result[j] = i; break; } } } for (int i = 0; i < n; i++) { if (result[i] != -1) { cout << jobs[result[i]].id << " "; } } return 0; }
Attempts:
2 left
💡 Hint
Check the comparator function used in sort.
✗ Incorrect
The comparator sorts jobs by profit ascending (lowest first), so the scheduling picks lower profit jobs first, resulting in output in ascending profit order.
🚀 Application
advancedCalculate Total Profit from Scheduled Jobs
Given jobs with deadlines and profits, what is the total profit after scheduling optimally?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Job { char id; int deadline; int profit; }; bool cmp(Job a, Job b) { return a.profit > b.profit; } int main() { vector<Job> jobs = {{'a', 4, 70}, {'b', 1, 80}, {'c', 1, 30}, {'d', 1, 100}, {'e', 3, 40}, {'f', 2, 20}}; sort(jobs.begin(), jobs.end(), cmp); int n = jobs.size(); vector<int> result(n, -1); vector<bool> slot(n, false); int total_profit = 0; for (int i = 0; i < n; i++) { for (int j = min(n, jobs[i].deadline) - 1; j >= 0; j--) { if (!slot[j]) { slot[j] = true; result[j] = i; total_profit += jobs[i].profit; break; } } } cout << total_profit; return 0; }
Attempts:
2 left
💡 Hint
Schedule jobs by descending profit and assign to latest free slot before deadline, summing profits.
✗ Incorrect
Scheduled jobs: d(100) slot 0, a(70) slot 3, e(40) slot 2, f(20) slot 1. Total profit = 100+70+40+20=230. Jobs b and c cannot be scheduled due to slot conflicts.
🧠 Conceptual
expertWhy Greedy Algorithm Works for Job Scheduling with Deadlines
Why does the greedy approach of sorting jobs by profit and assigning each to the latest free slot before its deadline produce an optimal solution?
Attempts:
2 left
💡 Hint
Think about how local choices affect the overall profit.
✗ Incorrect
The greedy method picks the highest profit job first and places it in the latest possible slot before its deadline, ensuring no higher profit job is displaced. This local optimization leads to a global maximum profit.
