Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort jobs by their profit in descending order.
DSA Python
jobs.sort(key=lambda x: x.[1], reverse=True)
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by deadline instead of profit.
Sorting in ascending order instead of descending.
✗ Incorrect
We sort jobs by their profit to schedule the most profitable jobs first.
2fill in blank
mediumComplete the code to find the maximum deadline among all jobs.
DSA Python
max_deadline = max(job.[1] for job in jobs)
Attempts:
3 left
💡 Hint
Common Mistakes
Using profit instead of deadline.
Using min() instead of max().
✗ Incorrect
The maximum deadline is found by checking the deadline attribute of each job.
3fill in blank
hardFix the error in the code that assigns jobs to slots.
DSA Python
for j in range(min(max_deadline, job.[1]) - 1, -1, -1):
Attempts:
3 left
💡 Hint
Common Mistakes
Using profit instead of deadline in the loop range.
Looping forwards instead of backwards.
✗ Incorrect
We check slots from the job's deadline backwards to find a free slot.
4fill in blank
hardFill both blanks to mark the slot as occupied and add the job's profit.
DSA Python
if slot[j] is None: slot[j] = job.[1] max_profit += job.[2] break
Attempts:
3 left
💡 Hint
Common Mistakes
Marking slot with profit instead of job id.
Adding deadline instead of profit to max_profit.
✗ Incorrect
We mark the slot with the job id and add the job's profit to total profit.
5fill in blank
hardFill all three blanks to complete the job scheduling function.
DSA Python
def job_scheduling(jobs): jobs.sort(key=lambda x: x.[1], reverse=True) max_deadline = max(job.[2] for job in jobs) slot = [None] * max_deadline max_profit = 0 for job in jobs: for j in range(min(max_deadline, job.[3]) - 1, -1, -1): if slot[j] is None: slot[j] = job.id max_profit += job.profit break return max_profit, slot
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing profit and deadline in wrong places.
Not sorting jobs before scheduling.
✗ Incorrect
Sort by profit, find max deadline, and use job deadline to find slot.
