Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort jobs by their deadlines in ascending order.
DSA Go
sort.Slice(jobs, func(i, j int) bool {
return jobs[i].Deadline [1] jobs[j].Deadline
})Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' will sort in descending order, which is incorrect for earliest deadline first.
Using '==' or '!=' does not define a proper order.
✗ Incorrect
Sorting jobs by ascending deadline means jobs with smaller deadlines come first, so we use '<'.
2fill in blank
mediumComplete the code to find the maximum deadline among all jobs.
DSA Go
maxDeadline := 0 for _, job := range jobs { if job.Deadline [1] maxDeadline { maxDeadline = job.Deadline } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' will find the minimum deadline, not maximum.
Using '==' or '!=' does not help find max value.
✗ Incorrect
We update maxDeadline when job.Deadline is greater than current maxDeadline.
3fill in blank
hardFix the error in the code that schedules jobs to maximize profit without missing deadlines.
DSA Go
slots := make([]bool, maxDeadline) result := make([]int, maxDeadline) for i := range result { result[i] = -1 } for i := 0; i < len(jobs); i++ { for j := jobs[i].Deadline - 1; j >= 0; j-- { if slots[j] == [1] { slots[j] = true result[j] = i break } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for true means slot is occupied, so job can't be assigned there.
Using nil or 0 is invalid for boolean comparison.
✗ Incorrect
We check if the slot is free (false) before assigning a job to it.
4fill in blank
hardFill both blanks to print the scheduled jobs in order of their slots.
DSA Go
for i := 0; i < maxDeadline; i++ { if result[i] != [1] { fmt.Println("Job ID:", jobs[result[i]].[2]) } }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 0 instead of -1 will miss unassigned slots.
Printing Deadline or Profit instead of ID is not the usual output.
✗ Incorrect
We skip slots with -1 (no job) and print the job's ID.
5fill in blank
hardFill all three blanks to calculate total profit from scheduled jobs.
DSA Go
totalProfit := 0 for i := 0; i < maxDeadline; i++ { if result[i] != [1] { totalProfit += jobs[result[i]].[2] } } fmt.Println("Total Profit:", [3])
Attempts:
3 left
💡 Hint
Common Mistakes
Using Deadline instead of Profit for summation.
Printing Deadline or jobs instead of totalProfit.
✗ Incorrect
We sum profits of scheduled jobs (not -1) and print totalProfit.
