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 Python code that schedules jobs to maximize profit given deadlines?
DSA Python
def job_scheduling(jobs): n = len(jobs) jobs.sort(key=lambda x: x[2], reverse=True) # Sort by profit descending result = [False] * n slot = [-1] * n for i in range(n): for j in range(min(n, jobs[i][1]) - 1, -1, -1): if not result[j]: result[j] = True slot[j] = jobs[i][0] break return [job for job in slot if job != -1] jobs = [('a', 2, 100), ('b', 1, 19), ('c', 2, 27), ('d', 1, 25), ('e', 3, 15)] print(job_scheduling(jobs))
Attempts:
2 left
💡 Hint
Sort jobs by profit descending and assign each job to the latest available slot before its deadline.
✗ Incorrect
The code sorts jobs by profit descending. It tries to schedule each job in the latest free slot before its deadline. The jobs 'a', 'c', and 'e' get scheduled, maximizing profit.
🧠 Conceptual
intermediateMaximum Number of Jobs Scheduled
Given the jobs with deadlines and profits, what is the maximum number of jobs that can be scheduled without missing deadlines?
Attempts:
2 left
💡 Hint
The maximum number of jobs scheduled equals the number of available time slots before deadlines.
✗ Incorrect
Since the maximum deadline is 3, at most 3 jobs can be scheduled without overlapping deadlines.
🔧 Debug
advancedIdentify the Error in Job Scheduling Code
What error will the following code produce when run?
DSA Python
def job_scheduling(jobs): n = len(jobs) jobs.sort(key=lambda x: x[2]) # Sort by profit ascending (incorrect) result = [False] * n slot = [-1] * n for i in range(n): for j in range(min(n, jobs[i][1]) - 1, -1, -1): if not result[j]: result[j] = True slot[j] = jobs[i][0] break return [job for job in slot if job != -1] jobs = [('a', 2, 100), ('b', 1, 19), ('c', 2, 27), ('d', 1, 25), ('e', 3, 15)] print(job_scheduling(jobs))
Attempts:
2 left
💡 Hint
Check the sorting order of jobs by profit.
✗ Incorrect
The code sorts jobs by profit ascending, so it schedules low profit jobs first, resulting in ['b', 'c', 'e'].
❓ Predict Output
advancedOutput of Job Scheduling with Overlapping Deadlines
What is the output of this code scheduling jobs with overlapping deadlines and profits?
DSA Python
def job_scheduling(jobs): n = len(jobs) jobs.sort(key=lambda x: x[2], reverse=True) result = [False] * n slot = [-1] * n for i in range(n): for j in range(min(n, jobs[i][1]) - 1, -1, -1): if not result[j]: result[j] = True slot[j] = jobs[i][0] break return [job for job in slot if job != -1] jobs = [('x', 1, 20), ('y', 2, 15), ('z', 2, 10), ('w', 1, 5)] print(job_scheduling(jobs))
Attempts:
2 left
💡 Hint
Schedule jobs by highest profit first and assign to latest free slot before deadline.
✗ Incorrect
Jobs 'x' and 'y' are scheduled because they have highest profits and fit in slots 0 and 1 respectively.
🧠 Conceptual
expertOptimal Strategy for Job Scheduling with Deadlines
Which strategy guarantees maximum total profit when scheduling jobs with deadlines and profits?
Attempts:
2 left
💡 Hint
Think about maximizing profit while respecting deadlines.
✗ Incorrect
Sorting by descending profit and placing each job in the latest free slot before its deadline ensures maximum profit.
